<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var num1 = parseFloat(55);
if (!isNaN(num1)) {
num1 = Math.round(num1*100)/100;
var str = num1.toString();
var rs = str.indexOf('.');
if (rs < 0) {
rs = str.length;
str += '.';
}
while (str.length <= rs + 2) {
str += '0';
}
console.log("将浮点数四舍五入,取小数点后2位,位数不够进行补位:");
console.log(str);
}
var a = 9.39893;
var f = 9;
var b = a.toFixed(2)
var c = Number(a.toString().match(/^\d+(?:\.\d{0,2})?/));
var d = Number(f.toString().match(/^\d+(?:\.\d{0,2})?/));
console.log(c)
console.log(f)
console.log('c:',typeof c)
alert(a.toFixed(2));
console.log(typeof a)
console.log(b)
console.log(typeof b)
var num4 = 55.3;
console.log(num4.toFixed(2));
var num5 = 55;
console.log(num5.toFixed(2));
</script>
</body>
</html>