Home  >  Article  >  Web Front-end  >  Analysis of the method of converting decimals to integers in JS

Analysis of the method of converting decimals to integers in JS

高洛峰
高洛峰Original
2017-01-09 14:58:551392browse

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!


Statement:
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