Rounds the value of a number to the specified precision.

Syntax

JSV_round(expN, expP)

Prototype

n/a

Returns

The round function returns the value of a number rounded to the specified precision.

Parameters

The round function uses the following arguments.

Argument Description
expN The number that is to be rounded.
expP The precision to which the rounding should occur.

Remarks

This function is different than the round method of the Math object which returns the value of a number rounded to the nearest integer. We are interested in overcoming JavaScript's limitations with decimal precision.

For more information on this topic, see the article Significant Digits by Aaron Weiss.

Dependencies

n/a

Example

The following lines of code

var f = 1.0/3.0;
document.write( "The number " + f + 
    " can be rounded to 3 decimal places and be displayed as " + 
    JSV_round(f,3) );
var f = 1.0/8.0;
document.write( "The number " + f + 
    " can be rounded to 2 decimal places and be displayed as " + 
    JSV_round(f,2) );

produce