@[toc]

Keep two decimal places

Change num to a variable where you want to keep two decimal places. If you want to find a way to finish reading this headline, if you want to know why, you can continue to read headline 2.

Take down the whole

Writing a


Math.floor(num* 100) / 100.

num = num.toFixed(2);

Copy the code

Write two


Math.trunc(num* 100) / 100

num = num.toFixed(2);

Copy the code

Notice the difference!

  • Math.floor() is rounded down

  • Math.trunc() removes the decimal part

Take up the whole


Math.ceil(num* 100) / 100.

num = num.toFixed(2);

Copy the code

rounded

Methods a

Math.round(num* 100) / 100.

num = num.toFixed(2);

Copy the code
Method 2

num = num.toFixed(2); // Replace num with num

 

Number.prototype.toFixed = function(d) {

       var s = this + "";

       if(! d) d =0;

       if(s.indexOf(".") = = -1) s += ".";

       s += new Array(d + 1).join("0");

       if(new RegExp("^ (- | \ \ +)? (\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)) {

              var s = "0" + RegExp.$2,

                     pm = RegExp.$1,

                     a = RegExp.$3.length,

                     b = true;

              if(a == d + 2) {

                     a = s.match(/\d/g);

                     if(parseInt(a[a.length - 1) >4) {

                            for(var i = a.length - 2; i >= 0; i--) {

                                   a[i] = parseInt(a[i]) + 1;

                                   if(a[i] == 10) {

                                          a[i] = 0; b = i ! =1;

                                   } else break;

                            }

                     }

                     s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2");

 

              }

              if(b) s = s.substr(1);

              return(pm + s).replace(/ \. $/."");

       }

       return this + "";

      

};

Copy the code

Why does this have to work?

So why don’t we just use.tofixed ()

Many people would say that.tofixed () is used to round, but.tofixed () uses the banker rounding method, but the banker rounding method is not consistent with our common sense of rounding, for example, in the picture below the car turned over. The inability to accurately round is a problem with.tofixed (), so we have to get rid of it.

Math. Round way

Math.round() rounds a number to the nearest whole number, which is what we mean by rounding, but it only takes the whole number part.

So we can double the number, use round() to get the rounded part of the whole number, and then divide by 100.


Math.round(num* 100) / 100.

Copy the code

However, this method has its drawbacks. It works when the number to be processed has less than the number of decimal places you want to reserve. 100.00 It will become 100 if you use this method

If you want to keep two decimal places, this can be combined with num = num.tofixed (2), which effectively prevents the problem mentioned above.


Math.round(num* 100) / 100.

num = num.toFixed(2);

Copy the code

If you can combine rounding, you can also combine rounding up and rounding down.


Math.floor(3.1415926 * 100) / 100; //3.14 Round down

Math.ceil(3.1415926 * 100) / 100; //3.15 Round up

Math.round(3.1415926 * 100) / 100; //3.14 Round off

Copy the code

But be careful!! Math.round(x), while it doesn’t cause accuracy problems, it does have a minor pitfall that is easy to overlook.

Here is its rounding strategy:

  • If the fractional part is greater than 0.5, round to the next integer with a greater absolute value.

  • If the fractional part is less than 0.5, round to the next integer with a smaller absolute value.

  • If the fractional part is equal to 0.5, round to the next integer in the direction of plus infinity.

 

So for math.round (-1.5), the result is -1, so negative numbers are converted to positive numbers before processing.

 

Redefine toFixed ()

Since toFixed() is the banker rounding method, which is the flaw of this method, we can write a new toFixed() with the following code. When we want to use toFixed() directly, we can copy this code into it.


Number.prototype.toFixed = function(d) {

       var s = this + "";

       if(! d) d =0;

       if(s.indexOf(".") = = -1) s += ".";

       s += new Array(d + 1).join("0");

       if(new RegExp("^ (- | \ \ +)? (\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)) {

              var s = "0" + RegExp.$2,

                     pm = RegExp.$1,

                     a = RegExp.$3.length,

                     b = true;

              if(a == d + 2) {

                     a = s.match(/\d/g);

                     if(parseInt(a[a.length - 1) >4) {

                            for(var i = a.length - 2; i >= 0; i--) {

                                   a[i] = parseInt(a[i]) + 1;

                                   if(a[i] == 10) {

                                          a[i] = 0; b = i ! =1;

                                   } else break;

                            }

                     }

                     s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2");

 

              }

              if(b) s = s.substr(1);

              return(pm + s).replace(/ \. $/."");

       }

       return this + "";

 

};

Copy the code