Home > Article > Web Front-end > Analysis of the method of converting decimals to integers in JS
The example in this article describes the method of converting JS decimals to integers. Share it with everyone for your reference, the details are as follows:
1. Convert decimals to integers
floor: go down
Math.floor(12.9999) = 12
ceil:Move forward
Math.ceil(12.1) = 13;
round: Rounding
Math.round(12.5) = 13
Math.round( 12.4) = 12
2. Decimal digit control
Retain to integer:
exam = Math.round(exam);
Reserve one decimal place:
exam = Math.round(exam * 10) / 10;
Retain two decimal places:
exam = Math.round(exam * 100) / 100;
Reserve three decimal places:
exam = Math.round(exam * 1000) /1000;
Others can be deduced in turn. . . .
I hope this article will be helpful to everyone in JavaScript programming.
For more analysis of JS decimal to integer methods, please pay attention to the PHP Chinese website!