What's the correct way to round a PHP string to 2 decimal places?
$number = "520"; // It's a string from a DB
$formatted_number = round_to_2dp($number);
echo $formatted_number;
The output should be 520.00
;
How should the round_to_2dp()
function definition be?
Use PHP number_format() function. e.g.
The output will be:
7,234,545,423.00
This will give you 2 number after decimal.
Number without round
Another more exotic way to solve this issue is to use
bcadd()
with a dummy value for the $right_operand of0
.round_to_2dp
is an user defined function, nothing can be done unless you posted the declaration of that functionHowever, my guess is doing this
number_format($number,2);
It will return 5.98 without rounding the number.