Re: [PHP] Formatting Decimals Further Help

2010-01-22 Thread Rick Dwyer
Hello List. In an earlier post, I received help with a custom function to round decimals off (the custom function provided by Adam Richardson is below). However in my MySQL db, when I have values with only 1 decimal point, I need the value PHP returns to display as 2. For example, 3.8

Re: [PHP] Formatting Decimals Further Help

2010-01-22 Thread tedd
Hello List. In an earlier post, I received help with a custom function to round decimals off (the custom function provided by Adam Richardson is below). However in my MySQL db, when I have values with only 1 decimal point, I need the value PHP returns to display as 2. For example, 3.8

Re: [PHP] Formatting Decimals Further Help

2010-01-22 Thread Rick Dwyer
On Jan 22, 2010, at 4:24 PM, tedd wrote: Hello List. In an earlier post, I received help with a custom function to round decimals off (the custom function provided by Adam Richardson is below). However in my MySQL db, when I have values with only 1 decimal point, I need the value PHP

Re: [PHP] Formatting Decimals

2010-01-22 Thread Nathan Rixham
Paul M Foster wrote: On Mon, Jan 11, 2010 at 04:56:03PM -0500, tedd wrote: --Rick: The above described rounding algorithm introduces more bias than simply using PHP's round() function, which always rounds down. IMO, modifying rounding is not worth the effort. The best rounding algorithm

Re: [PHP] Formatting Decimals Further Help

2010-01-22 Thread Nathan Rixham
Rick Dwyer wrote: On Jan 22, 2010, at 4:24 PM, tedd wrote: Hello List. In an earlier post, I received help with a custom function to round decimals off (the custom function provided by Adam Richardson is below). However in my MySQL db, when I have values with only 1 decimal point, I

Re: [PHP] Formatting Decimals Further Help

2010-01-22 Thread Rick Dwyer
On Jan 22, 2010, at 7:30 PM, Nathan Rixham wrote: Thanks Nathan I'll give it a shot. --Rick your doing the number format before the rounding.. here's a version of the function that should fit the bill: function round_to_half_cent( $value ) { $value *= 100; if( $value

Re: [PHP] Formatting Decimals Further Help

2010-01-22 Thread Nathan Rixham
Rick Dwyer wrote: On Jan 22, 2010, at 7:30 PM, Nathan Rixham wrote: Thanks Nathan I'll give it a shot. np - here's a more condensed version: function round_to_half_cent( $value ) { $value = ($value*100) + 0.3; $out = number_format( floor($value)/100 , 2 ); return $out

Re: [PHP] Formatting Decimals Further Help

2010-01-22 Thread Rick Dwyer
Thank you Nathan, This worked quite well. --Rick On Jan 22, 2010, at 8:10 PM, Nathan Rixham wrote: Rick Dwyer wrote: On Jan 22, 2010, at 7:30 PM, Nathan Rixham wrote: Thanks Nathan I'll give it a shot. np - here's a more condensed version: function round_to_half_cent( $value ) {

Re: [PHP] Formatting Decimals

2010-01-11 Thread Ryan Sun
$newprice = sprintf($%.2f, 15.109); On Sun, Jan 10, 2010 at 8:36 PM, Rick Dwyer rpdw...@earthlink.net wrote: Hello List. Probably an easy question, but I am not able to format a number to round up from 3 numbers after the decimal to just 2. My code looks like this: $newprice =

Re: [PHP] Formatting Decimals

2010-01-11 Thread Rick Dwyer
I have been asked to further modify the value to the nearest half cent. So if the 3rd decimal spot ends in 1 or 2, it gets rounded down to 0 If it ends in 3, 4, 5, 6 it gets rounded to 5. And if it 7, 8 or 9 it gets rounded up to full cents. Can this be done fairly easily? Not knowing PHP

Re: [PHP] Formatting Decimals

2010-01-11 Thread Paul M Foster
On Mon, Jan 11, 2010 at 02:55:33PM -0500, Rick Dwyer wrote: I have been asked to further modify the value to the nearest half cent. So if the 3rd decimal spot ends in 1 or 2, it gets rounded down to 0 If it ends in 3, 4, 5, 6 it gets rounded to 5. And if it 7, 8 or 9 it gets rounded up to

Re: [PHP] Formatting Decimals

2010-01-11 Thread tedd
At 2:55 PM -0500 1/11/10, Rick Dwyer wrote: I have been asked to further modify the value to the nearest half cent. So if the 3rd decimal spot ends in 1 or 2, it gets rounded down to 0 If it ends in 3, 4, 5, 6 it gets rounded to 5. And if it 7, 8 or 9 it gets rounded up to full cents. Can

Re: [PHP] Formatting Decimals

2010-01-11 Thread Rick Dwyer
On Jan 11, 2010, at 4:56 PM, tedd wrote: At 2:55 PM -0500 1/11/10, Rick Dwyer wrote: I have been asked to further modify the value to the nearest half cent. So if the 3rd decimal spot ends in 1 or 2, it gets rounded down to 0 If it ends in 3, 4, 5, 6 it gets rounded to 5. And if it 7, 8

Re: [PHP] Formatting Decimals

2010-01-11 Thread Mattias Thorslund
tedd wrote: At 2:55 PM -0500 1/11/10, Rick Dwyer wrote: I have been asked to further modify the value to the nearest half cent. So if the 3rd decimal spot ends in 1 or 2, it gets rounded down to 0 If it ends in 3, 4, 5, 6 it gets rounded to 5. And if it 7, 8 or 9 it gets rounded up to full

Re: [PHP] Formatting Decimals

2010-01-11 Thread Adam Richardson
On Mon, Jan 11, 2010 at 7:45 PM, Mattias Thorslund matt...@thorslund.uswrote: tedd wrote: At 2:55 PM -0500 1/11/10, Rick Dwyer wrote: I have been asked to further modify the value to the nearest half cent. So if the 3rd decimal spot ends in 1 or 2, it gets rounded down to 0 If it ends in

Re: [PHP] Formatting Decimals

2010-01-11 Thread Paul M Foster
On Mon, Jan 11, 2010 at 04:56:03PM -0500, tedd wrote: --Rick: The above described rounding algorithm introduces more bias than simply using PHP's round() function, which always rounds down. IMO, modifying rounding is not worth the effort. The best rounding algorithm is to look at the last

[PHP] Formatting Decimals

2010-01-10 Thread Rick Dwyer
Hello List. Probably an easy question, but I am not able to format a number to round up from 3 numbers after the decimal to just 2. My code looks like this: $newprice = $.number_format($old_price, 2, ., ,); and this returns $0.109 when I am looking for $0.11. I tried: $newprice =

Re: [PHP] Formatting Decimals

2010-01-10 Thread Mattias Thorslund
Testing this out a little: matt...@mumin:~$ php -r 'echo \$.number_format(0.109, 2, ., ,).\n;' $0.11 matt...@mumin:~$ php -r 'echo $.number_format(0.109, 2, ., ,).\n;' $0.11 matt...@mumin:~$ php -r 'echo $.number_format(0.109, 2, ., ,).\n;' $0.11 I think the $ should be escaped with a