Home JQuery四舍五入保留两位小数
Post
Cancel

JQuery四舍五入保留两位小数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function gettoDecimal(num) {
    var result = parseFloat(num);
    if (isNaN(result)) {
        return false;
    }
    result = Math.round(num * 100) / 100;
    var s_x = result.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }
    return s_x;
}

注:

  • indexOf() 方法对大小写敏感
  • 如果要检索的字符串值没有出现,则该方法返回-1
This post is licensed under CC BY 4.0 by the author.