Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Javascript Round Function

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 127
    Comment on it

    The Round function return a value towards the nearest integer.

    Syntax:-

    Math.round(number)
    

    number is any numeric expression.

    If the fractional portion of number is .5 or greater, the argument is rounded to the next highest integer. If the fractional portion of number is less than .5, the argument is rounded to the next lowest integer.

    For example:

    1. Math.round(25.9) //returns 26
    2. Math.round(25.2) //returns 25
    3. Math.round(-2.58) //returns -3
    

    If you want to display $25 in standard currency format below is the examples:

    var original = 28.453;
    
    1) //round "original" to two decimals
    var result = Math.round(original*100)/100  //returns 28.45
    
    
    2) // round "original" to 1 decimal
    var result = Math.round(original*10)/10  //returns 28.5
    
    
    3) //round 8.111111 to 3 decimals
    var result = Math.round(8.111111*1000)/1000  //returns 8.111
    

    In case you haven't understanding it, the formula to round any number to x decimal points is:

    1) Multiple the original number by 10^x (10 to the power of x)
    2) Apply Math.round() to the result
    3) Divide result by 10^x
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: