HuggieBear Posted November 4, 2010 Share Posted November 4, 2010 I've been wracking my brain trying to figure this simple problem out and I can't for the life of me figure out the best way to do it. I have the following array: $n = array('81.0', '80.5', '70.5', '67.0', '65.5'); I only want to display the numbers to one decimal place if it's not a whole number, like so. 81 80.5 70.5 67 65.5 I've tried looking for a solution with sprintf(), rtrim() and number_format(), but what's the best way to do it? Regards Rich Quote Link to comment https://forums.phpfreaks.com/topic/217729-formatting-floats/ Share on other sites More sharing options...
Andy-H Posted November 4, 2010 Share Posted November 4, 2010 $n = array('81.0', '80.5', '70.5', '67.0', '65.5'); foreach($n as $v) { if (strstr($v, '.')) { echo number_format($v, 1) . '<br >' . PHP_EOL; } else { echo $v . '<br >' . PHP_EOL; } } Quote Link to comment https://forums.phpfreaks.com/topic/217729-formatting-floats/#findComment-1130178 Share on other sites More sharing options...
Andy-H Posted November 4, 2010 Share Posted November 4, 2010 //EDIT Just realised what you were asking for. $n = array('81.0', '80.5', '70.5', '70', '71.569', '67.0', '65.5', '90'); foreach($n as $v) { echo (ctype_digit($v) || !substr($v, strpos($v, '.') + 1, 1) ? sprintf('$n = %0.0f', $v) : sprintf('$n = %0.1f', $v)) . '<br >' . PHP_EOL; } OUTPUT: $n = 81 $n = 80.5 $n = 70.5 $n = 70 $n = 71.6 $n = 67 $n = 65.5 $n = 90 Quote Link to comment https://forums.phpfreaks.com/topic/217729-formatting-floats/#findComment-1130182 Share on other sites More sharing options...
HuggieBear Posted November 4, 2010 Author Share Posted November 4, 2010 I can see what you've tried to do there, but it won't quite work as the 'if' condition will always return true. strstr() will always return the decimal point along with whatever follows it. I think for want of a better solution, I'll probably go with this. $n = array('81.0', '80.5', '70.5', '67.0', '65.5'); foreach($n as $v) { $parts = explode(".", $v); echo ($parts[1]) ? $v : $parts[0]; echo "<br />\n"; } Cheers Rich Quote Link to comment https://forums.phpfreaks.com/topic/217729-formatting-floats/#findComment-1130183 Share on other sites More sharing options...
HuggieBear Posted November 4, 2010 Author Share Posted November 4, 2010 Found a better way: $n = array('81.0', '80.5', '70.5', '70', '71.569', '67.0', '65.5', '90'); foreach($n as $v) { echo (ctype_digit($v) ? sprintf('$n = %0.0f', $v) : sprintf('$n = %0.1f', $v)) . '<br >' . PHP_EOL; } OUTPUT: $n = 81.0 $n = 80.5 $n = 70.5 $n = 70 $n = 71.6 $n = 67.0 $n = 65.5 $n = 90 That doesn't work either. Look at the first value in the list 81.0, should display as 81, it's the same for 67.0 Quote Link to comment https://forums.phpfreaks.com/topic/217729-formatting-floats/#findComment-1130185 Share on other sites More sharing options...
Andy-H Posted November 4, 2010 Share Posted November 4, 2010 I edited my post before I saw your posts, the edited version will work unless you have a number to multiple decimal places and the first decimal place is 0. ie. 80.025 => 80 This is correct as it would be rounded down anyway, however 80.063 => 80 This would not work correct in the code I posted, it should be 80.1 If this scenario won't occur in your code my edited post will work for you. Quote Link to comment https://forums.phpfreaks.com/topic/217729-formatting-floats/#findComment-1130189 Share on other sites More sharing options...
Andy-H Posted November 4, 2010 Share Posted November 4, 2010 Found a fix for that too: $n = array('81.0', '80.5', '70.5', '70.025', '70.063', '71.569', '67.0', '65.5', '90'); foreach($n as $v) { echo ( (ctype_digit($v) || !substr(sprintf('%0.1f', $v), strpos(sprintf('%0.1f', $v), '.') + 1)) ? sprintf('$n = %0.0f', $v) : sprintf('$n = %0.1f', $v) ) . '<br >' . PHP_EOL; } Output: $n = 81 $n = 80.5 $n = 70.5 $n = 70 $n = 70.1 $n = 71.6 $n = 67 $n = 65.5 $n = 90 Quote Link to comment https://forums.phpfreaks.com/topic/217729-formatting-floats/#findComment-1130197 Share on other sites More sharing options...
KevinM1 Posted November 4, 2010 Share Posted November 4, 2010 I think you're both over thinking it: <?php $n = array('81.0', '80.5', '70.5', '70', '71.569', '67.0', '65.5', '90'); foreach($n as $v) { $fv = (float)$v; // cast them as floats $iv = (int)$v; // also cast them as ints $decimal = ($iv - $fv); // obtain the decimal portion by finding the difference if($decimal < 0) // the difference is the fractional part, always 0 or lower { echo number_format($fv, 1) . "<br />"; } else { echo $iv . "<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/217729-formatting-floats/#findComment-1130203 Share on other sites More sharing options...
HuggieBear Posted November 4, 2010 Author Share Posted November 4, 2010 I think I'll mark this as solved as there are now a number of solutions. As for which one I'm going with, I haven't decided yet :-\ Cheers Rich Quote Link to comment https://forums.phpfreaks.com/topic/217729-formatting-floats/#findComment-1130217 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.