js number formatting

Here are a few methods for rounding decimal values ​​to integers: Math.ceil(), Math.floor(), and Math.round(). These three methods follow the following rounding rules respectively:
◎Math.ceil() performs rounding up, i.e. it always rounds the value up to the nearest integer;
◎Math.floor() performs rounding down, i.e. It always rounds the number down to the nearest integer;
◎Math.round() performs standard rounding, i.e. it always rounds the number down to the nearest integer (this is also what we learned about rounding in math class rule).

Here is an example of using these methods:

?
1
2
3
4
5
6
7
8
9
alert(Math.ceil(25.9)); //26
alert(Math.ceil(25.5)); //26
alert(Math.ceil(25.1)); //26
alert(Math.round(25.9)); //26
alert(Math.round(25.5)); //26
alert(Math.round(25.1)); //25
alert(Math.floor(25.9)); //25
alert(Math.floor(25.5)); //25
alert(Math.floor(25.1)); //25

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326445160&siteId=291194637