johnny44 Posted April 23, 2008 Share Posted April 23, 2008 This converts fractions to decimals, rounded to two decimal places. But with fractions like 8/2 or 21/10, the outputs are 4 and 2.1, whereas I need 4.00 and 2.10. Need two decimal places always. $numerator = 8; // say $denominator = 2; // say $output = round ( ($numerator/$denominator) , 2 ); echo $output; This outputs 4, but I need 4.00. I can get this done indirectly: $array = explode( "." , $output ); $n = strlen( $array[1] ); if( $n == 0 ){ $output = $output . ".00"; } elseif( $n == 1 ){ $output = $output . "0"; } elseif($n == 2 ){ } echo $output; But this strikes me as being totally ridiculous. Is there a straightforward function I could use to ensure that output is always expressed to two decimal places? Numerator and denominator will vary, but are always positive integers. Link to comment https://forums.phpfreaks.com/topic/102475-solved-outputting-200-and-130-rather-than-2-and-13/ Share on other sites More sharing options...
sasa Posted April 23, 2008 Share Posted April 23, 2008 look http://www.php.net/number-format Link to comment https://forums.phpfreaks.com/topic/102475-solved-outputting-200-and-130-rather-than-2-and-13/#findComment-524746 Share on other sites More sharing options...
johnny44 Posted April 23, 2008 Author Share Posted April 23, 2008 Gotcha, thanks! $output = number_format( $output , 2 , "." , "" ); Link to comment https://forums.phpfreaks.com/topic/102475-solved-outputting-200-and-130-rather-than-2-and-13/#findComment-524880 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.