Math Round function in JavaScript

We can round off any number with decimal place to an integer value by using round function. If the decimal portion of the number is equal or greater than .5 then the next higher integer is returned or if the number is less than 05 then the number is rounded to next lowest integer.

Here is the syntax.
Math.round(number)
Here are some examples.

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
We can format math number by using round function. Here is an example to format upto two decimal places.

var my_val=11.257;
var my_val=11.254;

document.write (Math.round(my_val*100)/100);
But we can use math toFixed function to format any number

Demo of Math round function

Here is the source of above demo
<script type="text/javascript">
function disp_data(){
var a1 = document.getElementById('t1').value;
a1=Math.round(a1);
document.getElementById("a2").innerHTML= a1;
}
</script>

<input type=text name=t1 id='t1' value=-5.678><input type=button value='Display rounded number' onClick='disp_data()';> 
<div id='a2'  style=" background-color: #c0f0c0; width:200" > Enter a decimal number</div>
You can read the php math round function
Absolute value of integer
JavaScript Math Reference
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer