I do my php work on my dev box at home, where I've got a rudimentary LAMP setup. When I look at my website on my home box, any numbers I echo are automatically truncated to the least required precision. Eg 2 is echoed as 2, 2.2000 is echoed as 2.2.
On the production box, all the numbers are echoed with at least one unnecessary zero, eg 100 becomes 100.0. On both boxes, the PHP version is 5.2.5. Does anyone know offhand if there is a setting I can change which will force PHP to automatically remove any unnecessary zeroes? I don't want to have to go to every place in my code where I output a number and replace echo with printf or something like that.
Muchas gracias.
Try the built in function
round
;Example #1 round() examples
Example #2 mode examples
A quick look through the available INI settings makes me thing your precision values are different?
Just to rule out other possible causes: Where are the numbers coming from? Does it do this with literal values?
It doesn't seem likely that the precision setting alone could cause this. Check also if anything might be interfering with the output via things like
auto_prepend_file
oroutput_handler
.You should use the round() command to always round down the precision you want, otherwise some day you'll get something like 2.2000000000123 due to the nature of float arithmetic.
And when you can't rely on the PHP configuration, don't forget about number_format() which you can use to define how a number is returned, ex:
PS: and try to avoid using money_format(), as it won't work on Windows and some other boxes
thanks for all the answers - the solution was to cast the return value of the method responsible to a float. I.e. it was doing
I just changed it to
then PHP truncated any trailing zeroes when required.
Can someone close this?