Converting from number to string gives strange number in Javascript [duplicate] - javascript

This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 8 years ago.
So I saw this strange scenario. I wanted to convert a number to a String in Node.js and I got the following.
01010100132.toString()
Turns into
"136347738"
Can someone explain this to me?

Any numeric constant prefixed with a 0 is an octal literal (assuming all its digits are valid octal digits).
var i = 010; // 8 decimal

Related

console.log weird behaviour with numbers [duplicate]

This question already has answers here:
Console.log output in javascript
(6 answers)
Closed 3 years ago.
console.log(01) results in 1
But
console.log(011) results in 9
Can someone explain how console.log works with such numbers?
It's not about console.log, a number that starts with 0 is octal notation
console.log(+"011") // if you use like this it will work
011 is an octal value and its decimal equivalent is 9. Preceding integer literal with 0 indicates octal value.

Why is js parseInt not working for this particular integer? [duplicate]

This question already has answers here:
How is the parseInt in JavaScript defined to handle large "numbers" - is there an ECMA leak? I got a wow here
(3 answers)
Why is parseInt() not converting my String of numbers correctly?
(1 answer)
Closed 4 years ago.
Here is my string: "6145390195186705543"
I have tried parseInt with different radix values. I have tried the Number() method. And I have tried multiplying it by one. They all give back 6145390195186705000
Why is this??

Number literal with leading zero does not result in expected value [duplicate]

This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 7 years ago.
I have this code:
console.log(066); // 54
Why does it log 54, not 66?
In JavaScript, numeric literals that begin with a 0 are treated as octal.
From the MDN docs:
Octal number syntax uses a leading zero. If the digits after the 0 are outside the range 0 through 7, the number will be interpreted as a decimal number.
Because add a prefix 0 will make the number to be considered of base 8(octal), as way 0x will make the following number to be of base 16(hexa)

toString() removes the zero at the right [duplicate]

This question already has answers here:
toString and valueOf truncates trailing 0s after decimal
(2 answers)
Closed 9 years ago.
I have a number, 2.0e-14, and I want to convert it to string. I'm using (2.0e-14).toString(), and what I get is the string 2e-14, because it's removing the zeroes at the right.
Is there any way of keep the zero at the right than to manipulating the string?
Use the Number toPrecision method.
(2.0e-14).toPrecision(2)

How come parseInt("08") = 0, parseInt("07") = 7 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript function parseInt() doesn't parse numbers with leading 0 correctly
Strange issues when parsing in JS occur.
parseInt("08")
//The result is: 0
parseInt("07")
//The result is: 7
Why is this happening?
Because of the 0 prefix. It tells Javascript the number is Octal, in base-8. 8 isn't a legal octal digit.
Use parseInt("8") instead, or as #Gumbo so correctly pointed out - parseInt("08", 10)

Categories

Resources