scenario

When the front-end computations, floating-point types are often too long, so a rounding method is needed to ensure the number of decimal places.

Function main() {console.log(0.06 * 40.08)} main()Copy the code

Results obtained:

1. Use toFixed

Function main () {let res = (1.12 * 40.08) console.log('toFixed before: '+ res + ', type: '+ typeof res) res = res.tofixed (2) console.log('toFixed after:' + res + ', type: '+ typeof res)} main()Copy the code

ToFixed can be rounded, but this returns a string, and when 10.1, it returns 10.10, as follows

Function main () {let res = (10 * 1.01) console.log('toFixed before: '+ res + ', type: '+ typeof res) res = res.tofixed (2) console.log('toFixed after:' + res + ', type: '+ typeof res)} main()Copy the code

ToFixed (2)), which converts the resulting string to a numeric type and removes the extra zeros, i.e. “10.10” becomes 10.1

Function main () {let res = (10 * 1.01) console.log('toFixed before: '+ res + ', type: '+ typeof res) res = res.tofixed (2) console.log('toFixed after:' + res + ', type: '+ typeof res) res = Number(res) console.log('Number after:' + res + ', type: '+ typeof res)} main()Copy the code

2. Use Math. Round (x)

The method is explained by rounding numbers to the nearest whole number.

Function main () {let res = (10 * 1.07) console.log('round: '+ res + ', type: '+ typeof res) res = math.round (res) console.log('round:' + res + ', type: '+ typeof res)} main()Copy the code

Now, the reader might wonder, this is not a decimal, it’s an integer, so we need to optimize it,

Multiply the decimal by 100, then round it, then divide it by 100, and you get what you want,

100 means to keep two decimal places.

Function main () {let res = (10 * 1.07) console.log('round: '+ res + ', type: '+ typeof res) res = math.round (res * 100) / 100 console.log('round after:' + res + ', type: '+ typeof res)} main()Copy the code

Here we can see that the output is already a standard 10.7 decimal.

Let’s test two decimal places again.

Function main () {let res = (10 * 1.07777) console.log('round: '+ res + ', type: '+ typeof res) res = math.round (res * 100) / 100 console.log('round after:' + res + ', type: '+ typeof res)} main()Copy the code

The rounded result is 10.78, which gives you the expected result.

To optimize the

The math.round method above achieves what we want, but multiplying by 100, dividing by 100, after all, is dead,

If we just want to keep one fixed decimal place, or three, or four, then we keep changing this factor, changing 100 to 10, 1000, whatever,

So we can encapsulate a little function that takes a few bits to pass a number.

Function main () {let res = (10 * 1.07777) console.log(' reserved 1 bit: '+ toFixed(res, 1)) console.log(' reserved 2 bits:') console.log(' reserved 2 bits: ') '+ toFixed(res, 2)) console.log(' save 3 bits:' + toFixed(res, 3)) console.log(' Save 4 bits: ') ' + toFixed(res, 4)) } function toFixed(number,length) { return Math.round(Math.pow(10, length) * number) / Math.pow(10, length) } main()Copy the code

As you can see, we have encapsulated a toFixed function, passing a specific value and the length to be retained to get the result we expect.

First, explain what Math.pow(x,y) means: returns x to the y power.

Function main () {console.log(" first power: "+ math.pow (10, 1)) console.log(" second power:" + math.pow (10, 2)) console.log(" third power: ") "+ math.h pow (10, 3)). The console log (" four power:" + Math. Pow (10, 4))} the main ()Copy the code

Math.round(math.pow (10, length) * number)/math.pow (10, length),

Math.round(100 * number) / 100 is equal to what we wrote above, except that we use the POW method so that the original factor of 100 can change dynamically depending on our parameters, which can be 10, 1000, or 10000.

expand

What if we don’t want to round? Discard decimals directly

Simply change math. round to math. floor

Math.floor(x): Rounds the numbers down.

Function main () {let res = (10 * 1.07777) console.log(' reserved 1 bit: '+ toFixed(res, 1)) console.log(' reserved 2 bits:') console.log(' reserved 2 bits: ') '+ toFixed(res, 2)) console.log(' save 3 bits:' + toFixed(res, 3)) console.log(' Save 4 bits: ') ' + toFixed(res, 4)) } function toFixed(number,length) { return Math.floor(Math.pow(10, length) * number) / Math.pow(10, length) } main()Copy the code

Readers can consult the various methods provided by the Math class for their own encapsulation extensions.