Number Formatting - Quick question !!

Number Formatting - Quick question !!

am 24.08.2007 09:40:54 von jerryyang_la1

I need some advice on formatting numbers using PHP.

My database holds numbers like:

10
02

I need these to be displayed as

10.00
020

I'll add the '=A3' sign, so the end result is

=A310.00
=A30.20

How do I do that. ? The vaule are coming from $number


Also some of the numbers may be negatives:

-0.3
-5

Is there any easy way to detect negitives ?

Ideally I'd like to be able to show positive number in one color and
negitive in red on screen.

Thanks :)

Re: Number Formatting - Quick question !!

am 24.08.2007 09:52:16 von Erwin Moller

jerryyang_la1@yahoo.com wrote:
> I need some advice on formatting numbers using PHP.
>
> My database holds numbers like:
>
> 10
> 0.2
>
> I need these to be displayed as
>
> 10.00
> 0.20
>
> I'll add the '£' sign, so the end result is
>
> £10.00
> £0.20
>
> How do I do that. ? The vaule are coming from $number
>
>
> Also some of the numbers may be negatives:
>
> -0.3
> -5
>
> Is there any easy way to detect negitives ?
>
> Ideally I'd like to be able to show positive number in one color and
> negitive in red on screen.
>
> Thanks :)
>

Hi,

Here is an example using sprintf, straight from www.php.net
http://nl3.php.net/manual/en/function.sprintf.php


$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
?>

Regards,
Erwin Moller

Re: Number Formatting - Quick question !!

am 24.08.2007 09:53:12 von Joe Scylla

jerryyang_la1@yahoo.com wrote:
> I need some advice on formatting numbers using PHP.
>
> My database holds numbers like:
>
> 10
> 0.2
>
> I need these to be displayed as
>
> 10.00
> 0.20
>

http://www.php.net/number_format

> Also some of the numbers may be negatives:
>
> -0.3
> -5
>
> Is there any easy way to detect negitives ?


if ($number < 0)
{
$color = "red";
}
else
{
$color = "green";
}


or


$color = ($number < 0) ? "red" : "green";


Joe