Jump to content

PHP number_format (Float)


romio

Recommended Posts

Hi,

 

i need to store the following in my database:

500,000

50,000

1500

30

1,5

 

after some research i understood that i need a float type, so i defined the following float (10,2)

 

the problem is that when i output the numbers:

 

echo number_format($number,2);

 

i get the following results:

 

200,000.00

50,000.00

1,500.00

30.00

1.50

 

i am not really happy with the output, is it possible to have sth like this:

 

200,000

50,000

1,500

30.00

1.50

 

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/156632-php-number_format-float/
Share on other sites

Why does 30 gets to keep the trailing zeros? Sorry, I don't see the pattern.

 

i am developing a small application that a winner might won the following amount:

 

if 5 numbers found then the prize is 100,000 Euro

if 4 ..... prize is 50,000 Euro

if 3 ..... prize is 1500 Euro

if 2 ..... prize is 30 Euro

if 1 ..... prize is 1,50 Euro

 

30 gets the trailing zeros bcoz its defined as float(10,2) even if i simply add 30 the trailing zeros will be added by MYSQL, and yes if you use only number_format() then 1,50 will be rounded up to 2.

One more thing, if you don't format it, what does it echo out? AKA echo $number.

 

The reason I ask is because how did number_format() turn 1,5 to 1.5?

 

My bad the records are saved in the table as follow:

 

100000.00

50000.00

1500.00

30.00

1.50

 

when i fetch the row from the table i echo it as follow:

 

$p1 = number_format($row->p1,2);

 

so the correct output is as follow:

 

300,000.00

50,000.00

1,500.00

30.00

1.50

 

Thanks

 

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.