Number.format Method
Formats a number according to a specific, user-defined format.
object.format ([NumDigitsAfterDecimal[, IncludeLeadingDigit[, UseParensForNegativeNumbers[, GroupDigits]]]])
Arguments
- object
- Required. Always the name of a Number object.
- NumDigitsAfterDecimal
- Optional. Numeric value indicating how many places to the right of the decimal
are displayed. If omitted, the original number of places to the right of the
decimal is used.
- IncludeLeadingDigit
- Optional. Boolean value that indicates whether or not a leading zero is
displayed for fractional values. Default is false.
- UseParensForNegativeNumbers
- Optional. Boolean value that indicates whether or not to place negative
values within parentheses. Default is false.
- GroupDigits
- Optional. Boolean value that indicates whether or not numbers are grouped. Default is false.
Return Value
The Number.format() method returns a string containing the number in the specified format.
Remarks
The following example illustrates the use of the Number.format method.
function FormatNumberDemo () {
var MyAngle, MySecant = new Number();
MyAngle = 1.3; // Define angle in radians.
MySecant = 1 / Math.cos(MyAngle); // Calculate secant.
return(MySecant.format(4)); // Format MySecant to four decimal places.
}
Requirements