php - print readable costs from stored decimal -
is there way php show 2 (or x) decimal places, not show .00 if whole number?
i've looked @ number_format, doesn't accommodates exactly.. though commas every 3 non-decimal places
34.00 => 34 34.7 => 34.70 12424.9=> 12,424.90 my numbers stored floats w/ 2 decimal places in database don't need that
what check if it's whole number first, rounding using floor() , checking whether that's equal full decimal version.
echo (floor($cost) == $cost) ? floor($cost) : number_format($cost, 2, '.', ','); or
if (floor($cost) == $cost) { echo floor($cost); } else { echo number_format($cost, 2, '.', ','); } another option use money_format():
echo (floor($cost) == $cost) ? floor($cost) : money_format('%.2n', $cost); or
if (floor($cost) == $cost) { echo floor($cost); } else { echo money_format('%.2n', $cost); }
Comments
Post a Comment