Home >Web Front-end >JS Tutorial >How to write js to retain the number of decimal places

How to write js to retain the number of decimal places

PHPz
PHPzOriginal
2016-05-16 17:05:371319browse

This article mainly introduces how to write several decimal places in js. Friends who need it can come and refer to it. I hope it will be helpful to everyone.

is as follows:

//保留小数点后2位
function disposeNumber(value){
    if(value == null || value == ""){
        return 0;
    }else if(value.toString().indexOf(".") == -1){
        return value;
    }else{
        return round(value, 2);
    }
}

function round(v,e){
    var t=1;
    for(;e>0;t*=10,e--);
    for(;e<0;t/=10,e++);
    return Math.round(v*t)/t;
}

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript video tutorial!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn