# JavaScript Number Methods
● Number Methods and Properties
Primitive values cannot have properties and methods.
But with JSP, methods & properties are also available to primitive values,
because JSP treats primitive values as objects when executing methods and properties.
● The toString() Method
toString() returns a number as a string.
All number methods can be used on any type of numbers.
● The toExponential() Method
toExponential() returns a string, with a number rounded and written using exponential notation. A parameter defines the number of characters behind the decimal point.
The parameter is optional. When I don't specify it, JSP will not round the number.
● The toFixed() Method
toFixed() returns a string, with the number written with a specified number of decimals.
● The valueOf() Method
valueOf() returns a number as a number.
In JavaScript, a number can be a primitive value (typeof = number) or an object (typeof = object).
The valueOf() method is used internally in JavaScript to convert Number objects to primitive values.
There is no reason to use it in my code. All JSP data types have a valueOf() and a toString() method.
valueOf() returns a number as a number.
● Converting Variables to Numbers
There are 3 JavaScript methods that can be used to convert variables to numbers.
- The Number() method
- The parseInt() method
- The parseFloat() method
These methods are not number methods, but global JavaScript methods.
These are the most relevant methods, when working with numbers.
- The Number() method
- The parseInt() method
- The parseFloat() method
These methods are not number methods, but global JavaScript methods.
http://www.w3schools.com/js/js_number_methods.asp |
● The Number() Method
Number() can be used to convert JavaScript variables to numbers.
Used on Date(), the Number() method returns the number of milliseconds since 1.1.1970.
● The parseInt() Method
parseInt() pares a string and returns a whole number. Spaces are allowed. Only the first number is returned.
● The parseFloat() Method
parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned.
If the number cannot be converted, NaN is returned.
● Number Properties
Number properties belongs to the JSP's number object wrapper called Number. These properties can only be accessed as Number.MAX_VALUE.
Using myNumber.MAX_VALUE, where myNumber is a variable, expression, or value, will return undefined.
http://www.w3schools.com/js/js_number_methods.asp |
No comments:
Post a Comment