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 need the value PHP returns to display as 2.  For example, 3.8 needs
>>> to display as 3.80.
>>>
>>> My line of code that calls the custom function looks like this:
>>>
>>> $my_price = round_to_half_cent(number_format($my_price, 3, '.', ','));
>>>

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



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to