In this example, the magnitude of the number is not large enough for an exponent being displayed by toString(). Results are mixed, because we reach the limits of the precision that can be achieved when converting binary numbers to a decimal notation: > 1234567890123456789012.toString() In this example, we force more precision when toString() would also use exponential notation. If it is omitted, then as many significant digits are included as necessary to uniquely specify the number. fractionDigits is a number between 0 and 20 that determines how many digits should be shown after the decimal point. (fractionDigits?) forces a number to be expressed in exponential notation. The last example gives us hope that we might be able to use parseInt() for converting numbers to integers. Thus, it is best to always explicitly state the radix, to always call parseInt() with two arguments.ĭon’t use parseInt() to convert a number to an integer. Additionally, some engines set the radix to 8 if str starts with a zero: > parseInt('010') So far I have described the behavior of parseInt() according to the ECMAScript specification. If radix is already 16, then the hexadecimal prefix is optional: > parseInt('0xA', 16) If radix is missing, then it is assumed to be 10, except if str begins with “0x” or “0X,” in which case radix is set to 16 (hexadecimal): > parseInt('0xA') If the radix is greater than 10, letters are used as digits (case-insensitively), in addition to 0–9. It determines the base of the number to be parsed. The range of the radix is 2 ≤ radix ≤ 36. As noted in Section 7.11, “Details of comparison predicates”: Note that this behavior is dictated by IEEE 754. Thus, it is best to combine isNaN with a type check: function myIsNaN ( value ) That conversion can produce NaN and then the function incorrectly returns true: > isNaN('xyz') However, isNaN does not work properly with nonnumbers, because it first converts those to numbers. If you want to check whether a value is NaN, you have to use the global function isNaN(): > isNaN(NaN) You therefore can’t search for NaN in an array via that method: >. NaN is the only value that is not equal to itself: > NaN = NaN
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |