MethodDescriptiontoString()Returns a number as a stringtoExponential()Returns a number written in exponential notationtoFixed()Returns a number written with a number of decimalstoPrecision()Returns a number written with a specified lengthValueOf()Returns a number as a number Show
The toString() MethodThe All number methods can be used on any type of numbers (literals, variables, or expressions): Examplelet x = 123; The toExponential() Method
A parameter defines the number of characters behind the decimal point: Examplelet x = 9.656; The parameter is optional. If you don't specify it, JavaScript will not round the number. The toFixed() Method
Examplelet x = 9.656;
The toPrecision() Method
Examplelet x = 9.656; The valueOf() Method
Examplelet x = 123; In JavaScript, a number can be a primitive value (typeof = number) or an object (typeof = object). The There is no reason to use it in your code. All JavaScript data types have a Converting Variables to NumbersThere are 3 JavaScript methods that can be used to convert a variable to a number: MethodDescriptionNumber()Returns a number converted from its argument.parseFloat()Parses its argument and returns a floating point numberparseInt()Parses its argument and returns a whole number The methods above are not number methods. They are global JavaScript methods. The Number() MethodThe ExampleNumber(true); If the number cannot be converted, The Number() Method Used on Dates
The The number of milliseconds between 1970-01-02 and 1970-01-01 is 86400000: The parseInt() Method
ExampleparseInt("-10"); If the number cannot be converted, The parseFloat() Method
ExampleparseFloat("10"); If the number cannot be converted, Number Object MethodsThese object methods belong to the Number object: MethodDescriptionNumber.isInteger()Returns true if the argument is an integerNumber.isSafeInteger()Returns true if the argument is a safe integerNumber.parseFloat()Converts a string to a numberNumber.parseInt()Converts a string to a whole number Number Methods Cannot be Used on VariablesThe number methods above belong to the JavaScript Number Object. These methods can only be accessed like Using X.isInteger() where X is a variable, will result in an error:
The Number.isInteger() MethodThe The Number.isSafeInteger() MethodA safe integer is an integer that can be exactly represented as a double precision number. The ExampleNumber.isSafeInteger(10); Safe integers are all integers from -(253 - 1) to +(253 - 1). The Number.parseFloat() Method
Spaces are allowed. Only the first number is returned: ExampleNumber.parseFloat("10"); If the number cannot be converted, NoteThe Number methods are the same as the Global methods The purpose is modularization of globals (to make it easier to use the same JavaScript code outside the browser). |