网页小技巧之保留小数

此以保留两位小数为例

思路:

法1:先把数字转换为字符串,使用indexOf()得到小数点在字符串位置,使用slice()或substr()截取字符串;

法2:数值乘100后取整除10

法3:直接使用方法toFixed();该方法自带四舍五入


关键知识:indedOf(),slice(),substr(),parseInt(),toFixed()
具体代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>保留两位小数点</title>
</head>
<body>
    
</body>
<script type="text/javascript">
    var PI = 3344.141592653;//定义常量
    var str = PI + '';//转换成字符串
    var index = str.indexOf('.');//得到小数点的位置
    console.log(str.slice(0,index+3));//从索引为0(第一个)开始取,取到小数点后两位
    console.log(str.substr(0,index+3));//从索引为0(第一个)开始取,取小数点的索引加3的个数

    console.log(parseInt(PI*100)/100);//乘100后取整再除以100
    console.log(PI.toFixed(2));//直接使用toFixed()方法保留两位小数
</script>
</html>

猜你喜欢

转载自blog.csdn.net/lyxuefeng/article/details/81674539