Math function round () Rounding example in JavaScript

Source: Internet
Author: User
Tags rounds


The round function can be rounded, it is a rounding function, the following is a look at the syntax of round:

1 Math.Round (number)
Let's look at a few examples:


The code is as follows
1 document.write (Math.Round (2.65));//Print 3
2 document.write (Math.Round (7.05));//Print 7
3 document.write (Math.Round ( -2.65));//Print-3
4 document.write (Math.Round ( -8.15));//Print-8
5 document.write (Math.Round (11.65));//Print 12


The rounding of the round function is only for the first digit after the decimal point, and if you want to target the rest of the decimal point, we can multiply the number by 10, and then divide the round by the same number:


The code is as follows
1 var my_val=11.257;
2 Var my_val=11.254;
3 document.write (Math.Round (my_val*100)/100);


JavaScript Round () method





Definitions and usage

The round () method rounds a number to the nearest integer.

Grammar

Math.Round (x)
Parameters Description
X Necessary. Must be a number.

return value

The nearest integer to X.

Description

For 0.5, the method is rounded up.

For example, 3.5 will be rounded to 4, and-3.5 will be rounded to-3.


Instance



Rounds a different number to the nearest integer:


The code is as follows

<script type= "Text/javascript" >

document.write (Math.Round (0.60) + "<br/>")
document.write (Math.Round (0.50) + "<br/>")
document.write (Math.Round (0.49) + "<br/>")
document.write (Math.Round ( -4.40) + "<br/>")
document.write (Math.Round (-4.60))

</script>

Output:

1
1
0
-4
-5


JavaScript Math.Round () function



Math.Round (x)--Returns the nearest integer to the number, rounded



Round, Chinese "rounding"




Round function syntax



Math.Round (x);



X--Numbers of type number



Round function return value



Returns the nearest integer for x, if the decimal part of X is greater than or equal to 0.5, the return value is the smallest integer greater than x, otherwise the round function returns the largest integer less than or equal to X
Round Function Example


The code is as follows

document.write (Math.Round (5.8));
document.write (Math.Round (5.2));
document.write (Math.Round (-5.8));
document.write (Math.Round (-5.2));

Results:

6
5
-6
-5

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.