- 保留小数位数
- Math.round(x*100)/100
将浮点数四舍五入,取小数点后2位
可以实现保留两位小数 - 强制保留两位小数
- Math.round(x*100)/100
function
toDecimal2
(
x
) {
var
f
=
parseFloat(x);
if
(isNaN(f)) {
return
false
;
}
var
f
=
Math.round(x
*
100
)
/
100
;
var
s
=
f.toString();
var
rs
=
s.indexOf(
'.'
);
if
(rs
<
0
) {
rs
=
s.length;
s
+=
'.'
;
}
while
(s.length
<=
rs
+
2
) {
s
+=
'0'
;
}
return
s;
}
function
fomatFloat
(
src
,
pos
){
return
Math.round(src
*
Math.pow(
10
, pos))
/
Math.pow(
10
, pos);
}
- Math.ceil()向上取整
- Math.floor()向上取整
- Math.round()四舍五入取整
- parseInt(x)直接取整
- X.toFixed(n);保留n位小数
- Math.power()n次方