0

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:

  1. If m is NaN, return the string "NaN".

  2. If m is +0 or -0, return the string "0".

  3. If m is less than zero, return the string concatenation of the string "-" and ToString(-m).

  4. If m is infinity, return the string "Infinity".

  5. 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.

  6. 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'.

  7. 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.

  8. 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.

  9. 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).

  10. 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?

user123444555621
  • 148,182
  • 27
  • 114
  • 126
user1039304
  • 365
  • 5
  • 10

3 Answers3

1

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 ToString operation can not be called directly from JS. You can get almost the same effect by this function:

  function ToString(input) {
      return '' + input;
  }

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:

ToString(100000000000000000000) // "100000000000000000000"
ToString(10000000000000000000000) // "1e+22"
ToString(.000003001) // ".000003001"
ToString(.0000003001) // "3.001e-7"
ToString(100000000.00000003001) // "100000000.00000003"
ToString(1000000000.00000003001) // "1000000000"

As for 5.: It can be deduced that

If m is an integer, n is the number of digits of m. s is the integer that results from stripping all trailing zeroes, and k is the number of digits in s.

So for example

  • m = 100000000000000000000 would yield s = 1, k = 1, n = 21, thus point 6. returns "100000000000000000000"
  • m = 10000000000000000000000 means s = 1, k = 1, n = 23, thus point 9. returns "1e+22"
user123444555621
  • 148,182
  • 27
  • 114
  • 126
  • thanks for your reply. according to 9.8.1 when the number value m is 100000000000000000000, n = k =21, s = 100000000000000000000, so m is converted to String according to the point 5 when the number value m is 10000000000000000000000, n = k = 23, s= 10000000000000000000000 is m converted to String according to the point 10? – user1039304 Apr 27 '12 at 09:23
  • @user1039304 I edited my answer to clarify point 5. – user123444555621 Apr 27 '12 at 11:47
  • i forgot that s is not divisible by 10... so m = 100000000.00000003001 would yield s = 10000000000000003001, k = 20, n = 9,thus point 7,return "100000000.00000003001" am i right? – user1039304 Apr 27 '12 at 14:53
  • 1
    Not quite. The thing is, the condition "the Number value for s×10^(n−k) is m" in 5. does not *exactly* mean "s*10^(n-k)==m". Instead, it accounts for rounding errors. See http://stackoverflow.com/questions/588004 – user123444555621 Apr 27 '12 at 21:37
0

you can get the best tutorial about toString at MDN

hope it will help .

Pranav
  • 8,563
  • 4
  • 26
  • 42
0

If you're just wanting to convert a number to a string, just use concatenation:

var number = 1;
var numberAsString = '' + number;
kroehre
  • 1,104
  • 5
  • 15