JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Math.round() Function

Name

Math.round() Function---round to the nearest integer

Availability

Navigator 2.0, Internet Explorer 3.0

Synopsis

Math.round(x)

Arguments

x

Any number or numeric expression

Returns

The integer closest to x.

Description

Math.round() rounds its argument up or down to the nearest integer.

Bugs

In Navigator 2.0, Math.round() did not correctly round very large numbers. The following workaround can be used:

function my_round(num) 
{
    var fl = Math.floor(num);
    var ce = Math.ceil(num);
    return (fl == ce) ? num :
           ((num - fl) < (num - ce)) ? fl : 
           ce;
}

See Also

"Math", "Math.ceil()", "Math.floor()"


Previous Home Next
Math.random() Book Index Math.sin()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
Hosted by uCoz