The round function in js can be used to round up numbers, which is calculated for 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:
The code copy is as follows:
Math.round(number)
Here are a few examples:
The code copy 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 of the round function is only for the first digit after the decimal point. If we want to target other digits after the decimal point, we can first multiply the number by an integer multiple of 10, and then divide by the same number after round:
The code copy is as follows:
var my_val=11.257;
var my_val=11.254;
document.write(Math.round(my_val*100)/100);
//print 11.25