i read the ecmascript 9.8.1 section ToString Applied to the Number Type,
but i don’t know what it means.
The operator ToString converts a number m to string format as follows:
If m is NaN, return the string “NaN”.
If m is +0 or -0, return the string “0”.
If m is less than zero, return the string concatenation of the string “-” and ToString(-m).
If m is infinity, return the string “Infinity”.
Otherwise, let n, k, and s be integers such that k >= 1, 10k-1<= s <10k, the number value for s * 10n-k is m, and k is as small as
possible. Note that k is the number of digits in the decimal
representation of s, that s is not divisible by 10, and that the least
significant digit of s is not necessarily uniquely determined by these
criteria.If k <= n <= 21, return the string consisting of the k digits of the decimal representation of s (in order, with no leading zeroes),
followed by n k occurrences of the character ‘0’.If 0 < n <= 21, return the string consisting of the most significant n digits of the decimal representation of s, followed by a
decimal point ‘. ‘, followed by the remaining k-n digits of the
decimal representation of s.If -6 < n <= 0, return the string consisting of the character ‘0’, followed by a decimal point ‘. ‘, followed by -n occurrences of the
character ‘0’, followed by the k digits of the decimal representation
of s.Otherwise, if k = 1, return the string consisting of the single digit of s, followed by lowercase character ‘e’, followed by a plus
sign ‘+ ‘ or minus sign ‘-‘ according to whether n-1 is positive or
negative, followed by the decimal representation of the integer
abs(n-1) (with no leading zeros).Return the string consisting of the most significant digit of the decimal representation of s, followed by a decimal point ‘. ‘,
followed by the remaining k-1 digits of the decimal representation of
s, followed by the lowercase character ‘e’, followed by a plus sign ‘+
‘ or minus sign ‘-‘ according to whether n-1 is positive or negative,
followed by the decimal representation of the integer abs(n-1) (with
no leading zeros).
can somebody explain the algorithm to me or give me a blog about the ToString Applied to the Number Type?
The specification is made for developers of JavaScript engines. It describes details of the engines’ internals, that may not be relevant to JS developers.
Particularly, the
ToStringoperation can not be called directly from JS. You can get almost the same effect by this function:Now section 9.8.1 simply describes what should happen when a number is passed into this function.
Points 5. to 10. make sure that very large numbers or numbers with many decimal places are stringified to scientific notation like so:
As for 5.: It can be deduced that
If
mis an integer,nis the number of digits ofm.sis the integer that results from stripping all trailing zeroes, andkis the number of digits ins.So for example
m = 100000000000000000000would yields = 1,k = 1,n = 21, thus point 6. returns"100000000000000000000"m = 10000000000000000000000meanss = 1,k = 1,n = 23, thus point 9. returns"1e+22"