A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearDefault Number of Decimal Places to Output in PHP - page 1
User InfoPosts
Default Number of Decimal Places to Output in PHP#1
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.

posted date: 2008-12-10 10:35:00


Re: Default Number of Decimal Places to Output in PHP#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-10 10:35:01


Re: Default Number of Decimal Places to Output in PHP#3
A quick look through the available INI settings makes me thing your precision values are different?

posted date: 2008-12-10 10:46:00


Re: Default Number of Decimal Places to Output in PHP#4
Tried that already, didn't work :(

posted date: 2008-12-10 10:49:00


Re: Default Number of Decimal Places to Output in PHP#5
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.

posted date: 2008-12-10 10:51:00


Re: Default Number of Decimal Places to Output in PHP#6
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.

posted date: 2008-12-10 10:52:00


Re: Default Number of Decimal Places to Output in PHP#7
@el_campo You need to try again, restart Apache, etc.. ;-)

posted date: 2008-12-10 15:06:00


Re: Default Number of Decimal Places to Output in PHP#8
yep did that all that ;-)

posted date: 2008-12-11 03:52:00


Re: Default Number of Decimal Places to Output in PHP#9
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 toreturn (float)someNumber.' grams';then PHP truncated any trailing zeroes when required.Can someone close this?

posted date: 2008-12-11 03:57:00


Re: Default Number of Decimal Places to Output in PHP#10
Try the built in function round;float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] );Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default). Example #1 round() examples<?phpecho round(3.4); // 3echo round(3.5); // 4echo round(3.6); // 4echo round(3.6, 0); // 4echo round(1.95583, 2); // 1.96echo round(1241757, -3); // 1242000echo round(5.045, 2); // 5.05echo round(5.055, 2); // 5.06?>Example #2 mode examples<?phpecho round(9.5, 0, PHP_ROUND_HALF_UP); // 10echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9?>

posted date: 2011-12-01 01:01:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.