Scenes
When the front-end calculates the amount, the floating-point type is 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()
come to conclusion:
1. Use toFixed
function main () {
let res = (1.12 * 40.08)
console.log('toFixed前: ' + res + ', 类型: ' + typeof res)
res = res.toFixed(2)
console.log('toFixed后: ' + res + ', 类型: ' + typeof res)
}
main()
Rounding can be achieved using toFixed, but this returns a string type, and when the value is 10.1, it returns 10.10, as follows
function main () {
let res = (10 * 1.01)
console.log('toFixed前: ' + res + ', 类型: ' + typeof res)
res = res.toFixed(2)
console.log('toFixed后: ' + res + ', 类型: ' + typeof res)
}
main()
But in the end, it can Number((10.1).toFixed(2))
be processed by converting the resulting string into a number type, and the extra 0 will disappear, that is, it will "10.10"
become10.1
function main () {
let res = (10 * 1.01)
console.log('toFixed前: ' + res + ', 类型: ' + typeof res)
res = res.toFixed(2)
console.log('toFixed后: ' + res + ', 类型: ' + typeof res)
res = Number(res)
console.log('Number后: ' + res + ', 类型: ' + typeof res)
}
main()
2. Use Math.round(x)
Explanation of this method: Round the number to the nearest whole number.
function main () {
let res = (10 * 1.07)
console.log('round前: ' + res + ', 类型: ' + typeof res)
res = Math.round(res)
console.log('round后: ' + res + ', 类型: ' + typeof res)
}
main()
Readers may have questions here. There is no decimal here, but directly becomes an integer, so we need to optimize it.
First multiply the decimal by 100, then round it, and finally divide by 100, and we will get the result we want,
100 means to keep two decimal places.
function main () {
let res = (10 * 1.07)
console.log('round前: ' + res + ', 类型: ' + typeof res)
res = Math.round(res * 100) / 100
console.log('round后: ' + res + ', 类型: ' + typeof res)
}
main()
Here we can see that the output is already a standard 10.7
decimal.
Test again for two decimal places.
function main () {
let res = (10 * 1.07777)
console.log('round前: ' + res + ', 类型: ' + typeof res)
res = Math.round(res * 100) / 100
console.log('round后: ' + res + ', 类型: ' + typeof res)
}
main()
The result after rounding is 10.78, which is the expected result.
optimization
Use the above Math.round
method to achieve the results we want, but multiplied by 100 and divided by 100, after all, was written dead,
If we only want to keep a fixed 1 decimal place, or 3 or 4 digits, then we have to keep changing this factor, changing 100 to 10 or 1000.
So we can encapsulate a small function, just need a few digits to pass a number.
function main () {
let res = (10 * 1.07777)
console.log('保留1位: ' + toFixed(res, 1))
console.log('保留2位: ' + toFixed(res, 2))
console.log('保留3位: ' + toFixed(res, 3))
console.log('保留4位: ' + toFixed(res, 4))
}
function toFixed(number,length) {
return Math.round(Math.pow(10, length) * number) / Math.pow(10, length)
}
main()
As you can see, we have encapsulated a toFixed
function, and only need to pass a specific value and the length that needs to be retained, and we can get the result we expect.
First explain Math.pow(x,y)
the meaning: return x to the power y.
function main () {
console.log("一次幂: " + Math.pow(10, 1))
console.log("二次幂: " + Math.pow(10, 2))
console.log("三次幂: " + Math.pow(10, 3))
console.log("四次幂: " + Math.pow(10, 4))
}
main()
In fact, what we used here Math.round(Math.pow(10, length) * number) / Math.pow(10, length)
,
It is equal to what we wrote above:, Math.round(100 * number) / 100
It is just the pow
method we use , so that the original factor of 100 can be dynamically changed according to our parameters, which can be 10, 1000, or 10000.
expand
What if we don’t want to round? Just discard the decimal
Simple, will use the above Math.round
change Math.floor
to,
Math.floor(x)
: Round the number down.
function main () {
let res = (10 * 1.07777)
console.log('保留1位: ' + toFixed(res, 1))
console.log('保留2位: ' + toFixed(res, 2))
console.log('保留3位: ' + toFixed(res, 3))
console.log('保留4位: ' + toFixed(res, 4))
}
function toFixed(number,length) {
return Math.floor(Math.pow(10, length) * number) / Math.pow(10, length)
}
main()
Readers can refer Math
to the various methods provided by the class and carry out their own package expansion.