What’s the correct way to round a PHP string to two decimal places?
solution 1.
You can use number_format()
return number_format((float)$number, 2, '.', '');
Example
$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00
This function returns a string.
solution 2.
echo round(520.34345, 2); // 520.34
echo round(520.3, 2); // 520.3
echo round(520, 2); // 520
Use round() (use if you are expecting a number in float format only.