当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js 保留两位小数

js 保留两位小数

2018年09月06日  | 移动技术网IT编程  | 我要评论

来自:https://blog.csdn.net/wd4java/article/details/50466633 侵删

 

以下我们将为大家介绍 javascript 保留两位小数的实现方法:
四舍五入
以下处理结果会四舍五入:

1
2
var num =2.446242342;
num = num.tofixed(2); // 输出结果为 2.45

不四舍五入
以下处理结果不会四舍五入:
第一种,先把小数边整数:

1
2
math.floor(15.7784514000 * 100) / 100 
// 输出结果为 15.77

第二种,当作字符串,使用正则匹配:

1
2
number(15.7784514000.tostring().match(/^\d+(?:\.\d{0,2})?/)) 
// 输出结果为 15.77,不能用于整数如 10 必须写为10.0000

注意:如果是负数,请先转换为正数再计算,最后转回负数

javascript保留两位小数的实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<script type="text/javascript">
    //保留两位小数 
    //功能:将浮点数四舍五入,取小数点后2位
    function todecimal(x) {
      var f = parsefloat(x);
      if (isnan(f)) {
        return;
      }
      f = math.round(x*100)/100;
      return f;
    }
  
  
    //制保留2位小数,如:2,会在2后面补上00.即2.00
    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);  
    }
    //四舍五入
    alert("保留2位小数:" + todecimal(3.14159267));
    alert("强制保留2位小数:" + todecimal2(3.14159267));
    alert("保留2位小数:" + todecimal(3.14559267));
    alert("强制保留2位小数:" + todecimal2(3.15159267));
    alert("保留2位小数:" + fomatfloat(3.14559267, 2));
    alert("保留1位小数:" + fomatfloat(3.15159267, 1));
      
    //五舍六入
    alert("保留2位小数:" + 1000.003.tofixed(2));
    alert("保留1位小数:" + 1000.08.tofixed(1));
    alert("保留1位小数:" + 1000.04.tofixed(1));
    alert("保留1位小数:" + 1000.05.tofixed(1));
      
    //科学计数
    alert(3.1415.toexponential(2));
    alert(3.1455.toexponential(2));
    alert(3.1445.toexponential(2));
    alert(3.1465.toexponential(2));
    alert(3.1665.toexponential(1));
    //精确到n位,不含n位
    alert("精确到小数点第2位" + 3.1415.toprecision(2));
    alert("精确到小数点第3位" + 3.1465.toprecision(3));
    alert("精确到小数点第2位" + 3.1415.toprecision(2));
    alert("精确到小数点第2位" + 3.1455.toprecision(2));
    alert("精确到小数点第5位" + 3.141592679287.toprecision(5));
  </script>

用javascript取float型小数点后两位,例22.127456取成22.13,如何做?

1.丢弃小数部分,保留整数部分

parseint(5/2)

2.向上取整,有小数就整数部分加1

math.ceil(5/2)

3,四舍五入.

math.round(5/2)

4,向下取整

math.floor(5/2)

另类的方法

1. 最笨的办法

 

复制代码 代码如下:

function get()
{
    var s = 22.127456 + "";
    var str = s.substring(0,s.indexof(".") + 3);
    alert(str);
}
 

 

2. 正则表达式效果不错

复制代码 代码如下:

< script type="text/javascript">
onload = function(){
    var a = "23.456322";
    var anew;
    var re = /([0-9]+.[0-9]{2})[0-9]*/;
    anew = a.replace(re,"$1");
    alert(anew);
}
< /script>

 

3. 他就比较聪明了.....

复制代码 代码如下:

< script>
var num=22.127456;
alert( math.round(num*100)/100);
< /script>

 

4.会用新鲜东西的朋友....... 但是需要 ie5.5+才支持。

5.js保留2位小数(强制)

对于小数点位数大于2位的,用上面的函数没问题,但是如果小于2位的,比如:changetwodecimal(3.1),将返回3.1,如果你一定需要3.10这样的格式,那么需要下面的这个函数:

 

复制代码 代码如下:

function changetwodecimal_f(x) {
    var f_x = parsefloat(x);
    if (isnan(f_x)) {
        alert('function:changetwodecimal->parameter error');
        return false;
    }
    var f_x = math.round(x * 100) / 100;
    var s_x = f_x.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;
}

 

 

 

功能:将浮点数四舍五入,取小数点后2位,如果不足2位则补0,
这个函数返回的是字符串的格式用法:changetwodecimal(3.1415926)返回3.14 changetwodecimal(3.1)返回3.10

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网