views:

1936

answers:

5

Hi all,

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.

+3  A: 

A quick look through the available INI settings makes me thing your precision values are different?

Peter Bailey
Tried that already, didn't work :(
el_champo
@el_campo You need to try again, restart Apache, etc.. ;-)
Till
yep did that all that ;-)
el_champo
+2  A: 

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:

// displays 3.14 as 3 and 4.00 as 4    
print number_format($price, 0); 
// display 4 as 4.00 and 1234.56 as 1,234.56 aka money style
print number_format($int, 2, ".", ",");

PS: and try to avoid using money_format(), as it won't work on Windows and some other boxes

TravisO
A: 

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

Ant P.
A: 

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.

+1  A: 

Hi guys, 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

return someNumber.' grams';

I just changed it to

return (float)someNumber.' grams';

then PHP truncated any trailing zeroes when required.

Can someone close this?

el_champo