百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> js保存两位小数的函数是什么
分享文章到:

js保存两位小数的函数是什么

发布时间:09/01 来源:未知 浏览: 关键词:

js保存两位小数的函数有:1、【toFixed()】函数;2、【Math.floor()】函数不四舍五入 ,向下取整;3、使用字符串匹配办法;4、四舍五入保存2位小数;5、浮点数保存两位小数。

js保存两位小数的函数有:

一、四舍五入相关

1、toFixed()办法

需留意,保存两位小数,将数值类型的数据改动成了字符串类型

// 1.四舍五入
     var num =2.446242342;  
    num = num.toFixed(2); 
   console.log(num); //2.45
  console.log(typeof num); // string

2、Math.floor(),不四舍五入 ,向下取整

留意,不改动数据类型

// 2.不四舍五入 向下取整
    num = Math.floor(num * 100) / 100;
     console.log(num); //2.44
    console.log(typeof num); // number

3、字符串匹配

留意,先将数据转换为字符串,最后再转为数值类型

// 3.不四舍五入 字符串匹配再转换
   num = Number(num.toString().match(/^\d+(?:\.\d{0,2})?/));
    console.log(num); //2.44
    console.log(typeof num); // number

4、四舍五入保存2位小数(若第二位小数为0,则保存一位小数)

留意,数据类型不变

//4.四舍五入保存2位小数(若第二位小数为0,则保存一位小数)
        function keepTwoDecimal(num) {
             var result = parseFloat(num);
             if (isNaN(result)) {
                 alert('传递参数错误,请检查!');
                 return false;
             }
             result = Math.round(num * 100) / 100;
             return result;
        };
         keepTwoDecimal(num);
         console.log(num); //2.44
         console.log(typeof num); //number

5、四舍五入保存2位小数(不足位数,则用0替补)

留意,数据类型变为字符串类型

//5.四舍五入保存2位小数(不足位数,则用0替补)
        function keepTwoDecimalFull(num) {
             var result = parseFloat(num);
             if (isNaN(result)) {
                 alert('传递参数错误,请检查!');
                 return false;
             }
             result = Math.round(num * 100) / 100;
             var s_x = result.toString(); //将数字转换为字符串
             var pos_decimal = s_x.indexOf('.'); //小数点的索引值
             // 当整数时,pos_decimal=-1 主动补0
             if (pos_decimal < 0) {
                 pos_decimal = s_x.length;
                 s_x += '.';
             }
             // 当数字的长度< 小数点索引+2时,补0
             while (s_x.length <= pos_decimal + 2) {
                 s_x += '0';
             }
             return s_x;
        }
         console.log(keepTwoDecimalFull(120.5)); //120.50
         console.log(typeof keepTwoDecimalFull(120.5)); //string
         console.log(keepTwoDecimalFull(2.446242342)); //2.45
         console.log(typeof keepTwoDecimalFull(2.446242342)); //string

二、浮点数保存两位小数

1、将浮点数四舍五入,取小数点后2位

留意,数据类型不变

//浮点数保存两位小数
          //1.功效:将浮点数四舍五入,取小数点后2位
          function toDecimal(x) {
           var f = parseFloat(x);
           if (isNaN(f)) {
            return;
           }
           f = Math.round(x*100)/100;
           return f;
          }
          console.log(toDecimal(3.1465926)); // 3.15
          console.log(typeof toDecimal(3.1415926)); //number

2、强迫保存2位小数,如:2,会在2后面补上00.即2.00

留意,数据类型变为字符串类型

//2.强迫保存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;
          }
          console.log(toDecimal2(3.1)); // 3.10
          console.log(typeof toDecimal2(3.1415926)); //string

3、保存两位小数 浮点数四舍五入 位数不足 不补0

留意,数据类型不变

// 3.保存两位小数 浮点数四舍五入 位数不足 不补0
          function fomatFloat(src,pos){
            return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
          }
           console.log(fomatFloat(3.12645,2)); // 3.13
          console.log(typeof fomatFloat(3.1415926)); //numbe
打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板