JS保留两位小数例子 四舍五入使用函数 toFixed()
[javascript]
<html>
<head>
</head>
<script>
var num = 22;
document.write("原来的值:" + num + "<br>");
document.write("两位小数:" + num.toFixed(2) + "<br>");
document.write("四位小数:" + num.toFixed(4) + "<br>");
</script>
<body>
</body>
</html>
[/javascript]
PHP保留两位小数例子 四舍五入
[php]
<?php
$num = 125664434.2;
echo number_format($num, 2).'<hr>'; //125,664,434.00
echo number_format($num, 2, ',', '').'<hr>'; //125664434,20
echo number_format($num, 2, '.', '').'<hr>'; //125664434.20
echo round($num, 2).'<hr>'; //125664434.2
echo sprintf("%.2f", $num).'<hr>'; //125664434.20
//保留两位小数并且不四舍五入
echo sprintf("%.2f", substr(sprintf("%.4f", $num), 0, -2)).'<hr>'; //125664434.20
?>
[/php]