JS中,如何判断一个数是不是小数?如果是小数,如何判断它是几位小数 保留n位小数

 

<script type="text/javascript">

    var x = 4.23323;//测试的数字
    var y = String(x).indexOf(".") + 1;//获取小数点的位置
    var count = String(x).length - y;//获取小数点后的个数
    if(y > 0) {
        alert("这个数字是小数,有" + count + "位小数");
    } else {
        alert("不是小数");
    }
</script>

 

//保留n位小数
function roundFun(value, n) {
    return Math.round(value*Math.pow(10,n))/Math.pow(10,n);
}

 

猜你喜欢

转载自www.cnblogs.com/wuss/p/11327026.html