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

Analysis of the method of converting decimals to integers in JS

高洛峰
Release: 2017-01-09 14:58:55
Original
1493 people have browsed it

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);
Copy after login

Reserve one decimal place:

exam = Math.round(exam * 10) / 10;
Copy after login

Retain two decimal places:

exam = Math.round(exam * 100) / 100;
Copy after login

Reserve three decimal places:

exam = Math.round(exam * 1000) /1000;
Copy after login

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!


Related labels:
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template