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

Link to comment
Share on other sites

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

 

 

 

 

 

 

Link to comment
Share on other sites

echo substr($row->p1, -3) == '.00'? number_format($row->p1) : number_format($row->p1, 2);

?

 

I know, but i thought if its possible having the result by using the number_format() only, thank you guys. i appreciate all your suggestions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.