Re: Preserving Decimals
| From: | php3 at developersdesk dot com | Date: | Sat, 17 Jun 2000 01:23:40 +0000 |
| Subject: | Re: Preserving Decimals | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-2114@lists.php.net to get a copy of this message | ||
Addressed to: "Yoed Anis" <yoed@zarcrom.com>
php-general@lists.php.net
** Reply to note from "Yoed Anis" <yoed@zarcrom.com> Fri, 16 Jun 2000 16:03:42 -0500
>
> Hello,
>
> PHP seems to do a funny thing when it adds empty decimals.. it makes
> them disappear. So if I were to add 5.00 to 4.00 it would display 9,
> not 9.00 Or if I were to add 4.50 to 3.00 it would display 8.5 not
> 8.50 I have looked through the archives and my book for a solution to
> this problem, since I thought it should be quiet common. After all
> PHP is used in many commerce sites.
>
> So what is the simplest way to preserve decimals in a variable?
>
How about:
number_format
(PHP3 , PHP4 )
number_format -- format a number with grouped thousands
Description
string number_format (float number, int decimals, string dec_point,
string thousands_sep)
number_format() returns a formatted version of number. This function
ccepts either one, two or four parameters (not three):
If only one parameter is given, number will be formatted
without decimals, but with a comma (",") between every group of
thousands.
If two parameters are given, number will be formatted with decimals
with a dot (".") in front, and a comma (",") between every group of
thousands.
If all four parameters are given, number will be formatted with
decimals decimals, dec_point instead of a dot (".") before the decimals
and thousands_sep instead of a comma (",") between every group of
thousands.
This has a side effect that 1234.50 will display as:
1,234.50
Rick Widmer
Internet Marketing Specialists
www.developersdesk.com