[pmwiki-devel] Simple PHP question

Patrick R. Michaud pmichaud at pobox.com
Mon Mar 12 17:12:06 CDT 2007


On Mon, Mar 12, 2007 at 06:08:57PM -0400, Sandy wrote:
> I want to convert floating point to strings and output them into the 
> HTML file. Round to two after the decimal point.
> 
>  From php.net, it looks like this is the right way:
> 
> $a=2.123456789;
> echo sprintf('%f5.2',$a);

That should be:

    echo sprintf('%5.2f', $a);

This means that you want two digits after the decimal point (".2"), 
and the value is to be padded on the left with spaces to achieve
a minimum of five characters ("5").

If you don't want any padding with spaces, just use %.2f as the
conversion specifier.

You might also look at PHP's round() function, which allows
rounding to other precisions.  http://www.php.net/round 

Pm



More information about the pmwiki-devel mailing list