There is a variable n which contains the number n = 084746473827;

When I use the tostring() function, tostring() already 84746473827 (that is, without 0 ).

Why it happens?

And how can a number lead to a string without changing it (for example, the number is 084746473827 , but it is necessary to return the string 084746473827 )?

  • four
    because the number is actually 84746473827 What makes you think that it costs 0 ? - Grundy
  • That is, you can not do to return with a zero as at the entrance? - Dementiy1999
  • one
    yes, only by hand. In this case, you should know what should be the final length. Where do you get the number from 0 ? - Grundy
  • In general, I receive a variable in which 084746473827 is a number. (checked typeof); but the answer below solved the problem. Thank you all for the clarification - Dementiy1999
  • 2
    the variable cannot be 084746473827 because, as you said this number, and it automatically discards leading zeros - Grundy

3 answers 3

Numbers do not count leading zeros. If you need to fill with zeros to a certain width, you can do this:

 var num, str; num = 84746473827; str = (new Array(12).join('0') + num).slice(-12); console.log(str); 

  • still as counted! - Igor
  • @Igor arguments? - tutankhamun
  • read other answers - Igor
  • @Igor Let's do it without riddles. I already read - tutankhamun
  • A leading zero means that the number is written in the octal system. - Igor

All wrong :). The zero at the beginning of the "literal" of the integer means that the number is written in the octal system. You are lucky that in the number there are numbers more than seven.

 // 017 is treated as octal var a = 017; console.log("a - decimal toString", a.toString()); console.log("a - octal toString", a.toString(8)); // 018 is treated as decimal var b = 018; console.log("b - decimal toString", b.toString()); 

If it's not hard to guess, then 084746473827 == 84746473827 because the first high byte starting from zero has no meaning

 let num = 084746473827; console.log(num) 

  • it's hard for me to guess - Igor