A
Number
object is created
using the Number()
constructor.It contains some useful methods, introduced in JavaScript 1.5.
- If the argument cannot be converted into a number, it returns NaN.
- In a non-constructor context (i.e., without the new operator, Number can be used to perform a type conversion.
new
Number(value);
Number (value);
Number (value);
Example:
var x=new
Number('12');
var y=new
Number('13');
alert(x+y); //returns 25
var x=new
Number('12d');
alert(x); //returns NaN
var x=
Number('12');
alert(x); /returns 12
Properties
Properties
|
Description
|
MAX_VALUE
|
The largest representable number in JavaScript.
Example:
alert(Number.MAX_VALUE);//1.7976931348623157e+308
var
x=1.7977931348623157e+308;
alert(x); //Infinity
|
MIN_VALUE
|
The smallest representable number in JavaScript.
alert(Number.MIN_VALUE);//5e-324
|
NaN
|
"Not a number" value.
|
NEGATIVE_INFINITY
|
Negative infinity, returned on overflow.
|
POSITIVE_INFINITY
|
Infinity, returned on overflow.
|
prototype
|
Prototype property, to add custom properties and methods to
this object.
|
Methods
Methods
|
Description
|
toExponential(x)
|
Returns a number in exponential notation (string format).
"x" should be a number between 0 and 20, dictating the number of
digits to include in the notation after the decimal place.
A string representing a Number object in exponential
notation with one digit before the decimal point, rounded to fractionDigits
digits after the decimal point. If the fractionDigits argument is omitted,
the number of digits after the decimal point defaults to the number of digits
necessary to represent the value uniquely.
If you use the toExponential() method for a numeric
literal and the numeric literal has no exponent and no decimal point, leave a
space before the dot that precedes the method call to prevent the dot from
being interpreted as a decimal point.
If a number has more digits that requested by the fractionDigits
parameter, the number is rounded to the nearest number represented by fractionDigits
digits.
|
toFixed(x)
|
Formats any number for "x" number of trailing
decimals. The number is rounded up, and "0"s are used after the
decimal point if needed to create the desired decimal length.
|
toPrecision(x)
|
Formats any number so it is of "x" length. Also
called significant digits. A decimal point and "0"s are used if
needed to create the desired length.
A string representing a Number object in fixed-point or
exponential notation rounded to precision significant digits. If the precision
argument is omitted, behaves as Number.prototype.toString().
If it is a non-integer value, it is rounded to the nearest integer.
If precison
is not between 1 and 100 (inclusive), a
RangeError
is thrown. Implementations are allowed to support larger and smaller values
as well. ECMA-262 only requires a precision of up to 21 significant digits. |
Examples
var profits=2489.8237
profits.toFixed(3); //returns 2489.824 (round up)
profits.toFixed(2); //returns 2489.82
profits.toFixed(3); //returns 2489.824 (round up)
profits.toFixed(2); //returns 2489.82
profits.toFixed(9); // 2489.823700000
var numObj = 5.123456;
console.log("numObj.toPrecision() is " + numObj.toPrecision()); //displays 5.123456
console.log("numObj.toPrecision(5)
is " + numObj.toPrecision(5)); //displays 5.1235
console.log("numObj.toPrecision(2)
is " + numObj.toPrecision(2)); //displays 5.1
console.log("numObj.toPrecision(1)
is " + numObj.toPrecision(1)); //displays 5
// note that exponential
notation might be returned in some circumstances
console.log((1234.5).toPrecision(2));
// "1.2e+3"
var numObj = 77.1234;
alert("numObj.toExponential()
is " + numObj.toExponential()); //displays 7.71234e+1
alert("numObj.toExponential(4)
is " + numObj.toExponential(4)); //displays 7.7123e+1
alert("numObj.toExponential(2)
is " + numObj.toExponential(2)); //displays 7.71e+1
alert("77.1234.toExponential()
is " + 77.1234.toExponential()); //displays 7.71234e+1
alert("77
.toExponential() is " + 77 .toExponential()); //displays 7.7e+1
No comments:
Post a Comment