Re: rounding and traling zeros

by "Ryan Fischer" <ryan(at)gigabee.com>

 Date:  Sat, 12 Apr 2003 18:39:57 -0400
 To:  "Keith D Sellars" <Keith(at)webgraffix.com>,
<hwg-languages(at)hwg.org>
 References:  S0026260871
  todo: View Thread, Original
> If I have the following in php:
> <?
>     $x .= .5 * $b * ((($a + (.25 * ($f - 25))) + $d + $e) * (1 + $g));
> ?>
>
> ... and I want the result ($x) to be rounded to 2 digits to the right
of the
> decimal place, BUT to ALWAYS show 2 digits (for instance, I don't want
> $24.50 to show as $24.5,), how would I do that?  I know that I can
use:
>
> round($x,2)
>
> to round the result to 2 digits, but how do I ensure that it it shows
the
> trailing zero if needed?
>
> Also, how do I incorporate the round($x,2) in the above calculation?
Should
> I simply create a second line that shows:
> $x = round($x,2)
> ... or will that even work at all?
>
> Many thanks in advance and please excuse my ingorance in asking what
may
> seem like simple questions.  I may not have the knowledge in my head,
but at
> least I know where the answers can be found!

$x = sprintf("%.2f", $x);

The "%.2f" is a number format, meaning to format the number passed as a
floating point number with two places after the decimal.

-Ryan Fischer

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA

This page is part of a preserved archive of archives.hwg.org. The site is no longer active and its content is not maintained. For enquiries about this archive, write to archive(at)iwanet.org.