Jump to content

[SOLVED] Outputting 2.00 and 1.30, rather than 2 and 1.3


johnny44

Recommended Posts

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.