An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Syntaxnew Number( value )
var thousand = new Number(1000);
console.log(thousand.valueOf() === 1000);
console.log(thousand !== 1000);
console.log(thousand == 1000);
var googol = new Number(1e+100);
console.log(googol.valueOf() === 1e+100);
var untydu = new Number(012);
console.log(untydu.valueOf() === 10);
function decimalToHex(d) {
var hex = Number(Math.abs(d)).toString(16);
hex = "000000".substr(0, 6 - hex.length) + hex;
return hex;
}
console.log(decimalToHex(127) === "00007f");
console.log(parseInt("00007f", 16) === 127);
var andBeyond = 3 / 0;
console.log(andBeyond === Infinity);
Remarks
JavaScript creates Number objects when a variable is set to a number value, for example var num = 255.336;
. It is seldom necessary to create Number objects explicitly.
The Number object has its own properties and methods, in addition to the properties and methods inherited from Object. Numbers are converted into strings under certain circumstances, for example when a number is added or concatenated with a string, as well as by means of the toString method. For more information, see Addition Operator (+).
JavaScript has several number constants. For a complete list, see Number Constants.
For a value that can not be converted to a number, the Number()
function returns the special value NaN (Not-a-Number) which indicates that the expression could not be evaluated to a number.
Only basic types such as strings and boolean values can be converted to numbers. Note, however, that a string can be converted to a number only if it is a numeric string:
Number("123");
Number("foo");
Number("123foo");
Integer range
Biggest int possible is 9007199254740992.
Smallest int possible is -9007199254740992.
var biggestInt = Math.pow(2, 53);
console.log(biggestInt + 1 === 9007199254740992) ;
console.log(biggestInt + 2 === 9007199254740994) ;
Octals and Hexadecimals
Octal (base-8) and hexadecimal (base-16) numbers can be used in JavaScript.
Octal numbers must begin with 0 (zero) followed by one or more octal digits.
Hexadecimal numbers must begin with 0x.
The following table lists the properties of the Number object.
Property Description constants Lists the constants of the Number object. constructor Specifies the function that creates an object. prototype Returns a reference to the prototype for a class of number. MethodsThe following table lists the methods of the Number object.
Method Description toExponential Returns a string that contains a number represented in exponential notation. toFixed Returns a string that represents a number in fixed-point notation. toPrecision Returns a string that contains a number that is represented in either exponential or fixed-point notation and that has a specified number of digits. toString Returns a string representation of an object. valueOf Returns the primitive value of the specified object. See also Related articles Javascript Other articles External resourcesECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011
AttributionsMicrosoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4