Home > Article > Backend Development > Example code sharing of PHP retaining 2 decimal places
JS retains two decimal places example to round using function toFixed()
document.write("
var a=2.1512131231231321;
document.write("Original value:" + a + "
");
document.write("Two decimal points:" + a.toFixed(2) + "
Four decimal points" + a.toFixed(4));
PHP rounding to two decimal places example
< ;?php
/**1.number_format*/
$number = 1234.5678;
$nombre_format_francais = number_format($number, 2, ',', ' ' ); // 1234,57
$english_format_number = number_format($number, 2, '.', ''); // 1234.57 (I usually use this)
/**2.round*/
$number = 1234.5678;
echo round($number ,2); //1234.57
/**3.sprintf*/
$formatted = sprintf ("%s has ¥%01.2f.", $name, $money);
echo $formatted; // Zhang San has ¥123.10.
?>
The above is the detailed content of Example code sharing of PHP retaining 2 decimal places. For more information, please follow other related articles on the PHP Chinese website!