Home > Web Front-end > JS Tutorial > body text

What are the js functions that retain two decimal places?

coldplay.xixi
Release: 2020-09-28 09:27:58
Original
26960 people have browsed it

js functions that retain two decimal places include: 1. [toFixed()] function; 2. [Math.floor()] function does not round, but rounds down; 3. Use string matching method; 4. Round to 2 decimal places; 5. Float to 2 decimal places.

What are the js functions that retain two decimal places?

js functions that retain two decimal places are:

1. Rounding related

1. toFixed() method

Please note that two decimal places are retained and the numerical type data is changed into a string type

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

2. Math.floor() , do not round, round down

Note, do not change the data type

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

3. String matching

Note, first convert the data into a string, and finally Convert to numeric type

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

4. Round and keep 2 decimal places (if the second decimal place is 0, keep one decimal place)

Note that the data type remains unchanged

//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
Copy after login

5. Round to keep 2 decimal places (if there are not enough digits, use 0 as substitute)

Note that the data type changes to string type

//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 += &#39;.&#39;;
             }
             // 当数字的长度< 小数点索引+2时,补0
             while (s_x.length <= pos_decimal + 2) {
                 s_x += &#39;0&#39;;
             }
             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
Copy after login

2. Keep two decimal places for floating point numbers decimal places

1. Round the floating point number to 2 decimal places

Note that the data type remains unchanged

//浮点数保留两位小数
          //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
Copy after login

2. Force 2 decimal places to be retained. For example: 2, 00 will be added after 2. That is, 2.00

Note that the data type changes to string type

//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(&#39;.&#39;);
           if (rs < 0) {
            rs = s.length;
            s += &#39;.&#39;;
           }
           while (s.length <= rs + 2) {
            s += &#39;0&#39;;
           }
           return s;
          }
          console.log(toDecimal2(3.1)); // 3.10
          console.log(typeof toDecimal2(3.1415926)); //string
Copy after login

3. Keep two decimal places and the floating point number will not be filled if there are not enough rounding digits. 0

Note that the data type remains unchanged

// 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
Copy after login

More related free learning recommendations: javascript video tutorial

The above is the detailed content of What are the js functions that retain two decimal places?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!