EngineeringGuy Posted October 22, 2006 Share Posted October 22, 2006 I am writing a function to convert a table of numbers from decimal e.g. (4.25) to fractional form (4 1/2) Certain columns of the table need to be ignored because they contain a letter or there is no fractional equivalent. The fractional form is to a 1/16 accuracy. (You can see a sample table here: [url=http://www.piping-designer.com/pages/flanges/fl150wnrtj.php]http://www.piping-designer.com/pages/flanges/fl150wnrtj.php[/url])I wrote the following function. My problem is this, when I search for items that are not listed in the array, it returns just the integer and that is unacceptable for this project. I have tried creating a fraction with 16ths as a denominator and reducing the fraction using the Euclidian algorithm but I reached same problem when the fraction would not reduce. How can I get the function to exit if $key is not found in the array? Here's the code:[code]<?phpfunction fraction($value){ // Define arrray that we are checking against$values = array('1/16' => .0625, '1/8' => .125, '3/16' => .1875, '1/4' => 0.25, '5/16' => .3125, '3/8' => .375, '7/16' => .4375, '1/2' => .5, '9/16' => .5625, '5/8' => .625, '11/16' => .6875, '3/4' => .75, '13/16' => .8125, '7/8' => .875, '15/16'=>.9375);if (eregi('[[:alpha:]]', $value)) { return $value; //contains any letter letter we're done here}else{ //There are no letters so we continue $intnum=floor($value); //take the value and round it down $dec = $value - $intnum; //Get the decimal portion of the original value $key = array_search($dec, $values,strict); //search the array for the decimal valueif ($intnum < 1){ //if the value is less than zero, then we will return just a fraction $value = $key;}else{ //otherwise we will return the integer and the fraction $value = $intnum." ".$key;} return $value; } }?>[/code]EDIT: Tried to fix formatting but failed horribly Quote Link to comment Share on other sites More sharing options...
LazyJones Posted October 22, 2006 Share Posted October 22, 2006 it return "just the integer" because this line[code]$value = $intnum." ".$key;[/code]gives just rounded up value (array_search() returns FALSE, so $key is empty)You can check if array_seach() returns FALSE and return if it does ( return the original value, or what ever you want) Quote Link to comment Share on other sites More sharing options...
EngineeringGuy Posted October 22, 2006 Author Share Posted October 22, 2006 Thanks a million! I put this in: if ($key){ and it worked like a charm! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 22, 2006 Share Posted October 22, 2006 try[code]<?phpfunction nearestFraction ($input) { $values = array('' => 0.0, '1/16' => .0625, '1/8' => .125, '3/16' => .1875, '1/4' => 0.25, '5/16' => .3125, '3/8' => .375, '7/16' => .4375, '1/2' => .5, '9/16' => .5625, '5/8' => .625, '11/16' => .6875, '3/4' => .75, '13/16' => .8125, '7/8' => .875, '15/16'=>.9375, '1'=> 1.0); $int = (int)floor($input); $dec_in = $input - $int; $min = 1.0; $nearfrac = ''; foreach ($values as $frac => $dec) { if (abs($dec - $dec_in) < $min) { $min = abs($dec - $dec_in); $nearfrac = $frac; } } return ($nearfrac == '1') ? $int++ : "$int $nearfrac";}echo nearestFraction(5.905);?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.