Home >Web Front-end >JS Tutorial >js rounding mathematical function round usage examples_basic knowledge

js rounding mathematical function round usage examples_basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:48:591381browse

The round function in js can be used to round numbers. It calculates the first digit after the decimal point.
The round function can round numbers. It is a rounding function. Let’s take a look at the syntax of round:

Copy code The code is as follows:

Math.round(number)

Let’s look at some examples:
Copy the code The code is as follows:

document.write(Math.round(2.65));// print 3
document.write(Math.round( 7.05));// print 7
document.write(Math.round(-2.65));// print -3
document.write(Math.round(-8.15));// print -8
document.write(Math.round(11.65));// print 12

The rounding function only targets the first digit after the decimal point. If you want to target other digits after the decimal point, We can first multiply the number by an integer multiple of 10, round and then divide by the same number:
Copy code The code is as follows:

var my_val=11.257;
var my_val=11.254;
document.write(Math.round(my_val*100)/100);
//print 11.25
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