Forum Moderators: coopster

Message Too Old, No Replies

Why is PHP/MySQL is stripping the zero off my rates?

         

DigitalSky

6:49 pm on Jun 14, 2010 (gmt 0)

10+ Year Member



Long story short...

I'm modifying some code I found to allow a user to input a new rate (i.e. 4.50). The code will also store that value in a variable and allow it to be output anywhere else on the site.

Anyhow, here's what's happening...

When I put 4.50 for my rate it strips off the zero and makes it 4.5. If I put 4.51 however it does not strip off the one and keeps the rate at 4.51.

I can't figure out why it's stripping the zero from the end but I don't want it to do this. I am searching through the code relentlessly and starting to think that maybe this is something that's controlled by the actual MySQL table structure?

LifeinAsia

6:53 pm on Jun 14, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



For most people and applications, 4.5=4.50. If you want to show "4.50" on your site, you will need to format the number to display with 2 decimal places (take a look at the number_format function).

DigitalSky

11:33 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



So are you saying that by default MySQL strips a floating number of any trailing zeros? There is no way around this simply by changing the type of row?

rocknbil

1:27 am on Jul 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_POST['problem_number'] = sprintf("%.2f",$_POST['problem_number']);

rocknbil

5:28 pm on Jul 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



by default MySQL


Wait a sec, sorry - is it a decimal field?

alter table yourtable change problem_number problem_number decimal(12,2) default '0.00';

But if you perform any math on 4.50, PHP will likely change it back to 4.5, use sprintf or number_format() as suggested.