Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 24th, 2007, 08:45 AM
jerryyang_la1@yahoo.com
Guest
 
Posts: n/a
Default Number Formatting - Quick question !!

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 :)

  #2  
Old August 24th, 2007, 08:55 AM
Joe Scylla
Guest
 
Posts: n/a
Default Re: Number Formatting - Quick question !!

jerryyang_la1@yahoo.com wrote:
Quote:
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
Quote:
Also some of the numbers may be negatives:
>
-0.3
-5
>
Is there any easy way to detect negitives ?
<code>
if ($number < 0)
{
$color = "red";
}
else
{
$color = "green";
}
</code>

or

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

Joe
  #3  
Old August 24th, 2007, 08:55 AM
Erwin Moller
Guest
 
Posts: n/a
Default Re: Number Formatting - Quick question !!

jerryyang_la1@yahoo.com wrote:
Quote:
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


<?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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles