Re: Formatting Decimals Further Help

From: Date: Sat, 23 Jan 2010 00:57:55 +0000
Subject: Re: Formatting Decimals Further Help
References: 1 2 3 4 5 6 7 8 9 10  Groups: php.general 
Request: Send a blank email to php-general+get-301366@lists.php.net to get a copy of this message
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 == (int)$value || $value < ((int)$value)+0.3 ) { return number_format( (int)$value/100 , 2); } else if($value > ((int)$value)+0.6) { return number_format( (int)++$value/100 , 2); } return number_format( 0.005+(int)$value/100 , 3); }
echo round_to_half_cent( 12.1 );       // 12.10
echo round_to_half_cent( 12.103 );     // 12.105
echo round_to_half_cent( 12.107 );     // 12.11
echo round_to_half_cent( 123456.789 ); // 123,456.79


« previous php.general (#301366) next »