-1

If I try to convert 003050 to a string it turns it into 1576 how can I turn 003050 into a string without it doing that. And any other possible whole number? I tried '' + 003050 and it's still 1576

String(003050);
"1576"
Steven
  • 13,250
  • 33
  • 95
  • 147
  • `parseInt(003050, 10) + "" == 1576` – Steven May 26 '14 at 15:03
  • try before post. 003050 is first interpreted as octal number which is 1576 in base 10. After that is just trying to parse 1576 to base 10 ( very helpfull right? ) – Alexandru Chichinete May 26 '14 at 15:05
  • You shouldn't have an integer with leading zeroes. Start off with that ) Otherwise there are tricks listed [in this topic here](http://stackoverflow.com/questions/2998784/how-to-output-integers-with-leading-zeros-in-javascript) – Jurijs Kastanovs May 26 '14 at 15:06

2 Answers2

5

This has no relation with the conversion to string.

var n = 003050; 

is enough to make the number interpreted as octal.

You would have gotten the same result with ""+parseInt("3050", 8).

Simply remove the leading 0 to get the number 3050 :

var n = 3050;

If you want a literal string with leading 0, well, just make it

var s = "003050";
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Is there no way, to convert it to string as is? – Amit Joki May 26 '14 at 15:03
  • 1
    So you're telling me the only way to make this number a string is to remove the zero's at the start, convert it to a string. Do what I need to do too it. Convert it back to a string. And re-add zeros? wow... – Steven May 26 '14 at 15:04
  • 1
    What's the exact goal ? Why not simply "003050" ? – Denys Séguret May 26 '14 at 15:04
  • Convert it to a string, so I can run my splice on it to generate 00:30:50, it's stored without the `:`'s due to FB. – Steven May 26 '14 at 15:05
  • 4
    If `003050` is not an octal, it's not a number. You can wrap `""` around it to make it a string, which seems what it is. – Jared Farrish May 26 '14 at 15:05
  • Maybe you are looking for that: http://stackoverflow.com/questions/5366849/convert-1-to-0001-in-javascript – user1520494 May 26 '14 at 15:10
1

Seems you could do with reading a little about Javascript,

Reference: Standard ECMA-262 5.1 Edition / June 2011

and understand primitives, objects and literals etc.

It is worth noting Additional Syntax

Past editions of ECMAScript have included additional syntax and semantics for specifying octal literals and octal escape sequences. These have been removed from this edition of ECMAScript. This non-normative annex presents uniform syntax and semantics for octal literals and octal escape sequences for compatibility with some older ECMAScript programs.

What you have, 003050, is a Numeric Literal

The syntax and semantics of 7.8.3 can be extended as follows except that this 
extension is not allowed for strict mode code:

Syntax

NumericLiteral ::
    DecimalLiteral
    HexIntegerLiteral
    OctalIntegerLiteral
OctalIntegerLiteral ::
    0 OctalDigit
    OctalIntegerLiteral OctalDigit
OctalDigit :: one of
0 1 2 3 4 5 6 7

And so, if not using strict mode, where this syntax has been removed ,then 003050 (an octal) is the decimal value 1576.

If you had, '003050', then you would have a String Literal

A string literal is zero or more characters enclosed in single or double quotes. Each character may be represented by an escape sequence. All characters may appear literally in a string literal except for the closing quote character, backslash, carriage return, line separator, paragraph separator, and line feed. Any character may appear in the form of an escape sequence.

So, you really wanted to use a string literal, didn't you?

If you really meant to use an octal numeric literal and wanted it as a string, then you would need to do something like this.

Javascript

var x = 003050,
    y = ('000000' + x.toString(8)).slice(-6);

console.log(y);

Output

003050

On jsFiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79