Number(str) getting converted to 1e+30
Answered
Anay-208 posted this in #help-forum
Anay-208OP
I have a string consisting of really large number, and I need to convert it to
Its a leetcode challenge, so I can't use any external modules
Number, so I can perform addition to it. However, when I attempt to convert it to number, I get: 1e+30.Its a leetcode challenge, so I can't use any external modules
12 Replies
Anay-208OP
bump
Western paper wasp
The number is the same under the hood, the default JS string representation just prints it as “3e+23†to save space
you can observe the same thing even if you type the number yourself (instead of parsing it from a string)
You could try toLocaleString if you need to convert the number back to a string without scientific notation
@Western paper wasp The number is the same under the hood, the default JS string representation just prints it as “3e+23†to save space
Anay-208OP
What can I do of it? I need to perform addition between 2 nums, in which one of them gets converted to 1e+30
10^30 range is too big to be safely used in the number type
But before that, ask yourself: why do you need to deal with so large a number
@joulev But before that, ask yourself: why do you need to deal with so large a number
Anay-208OP
Leetcode challenge: https://leetcode.com/problems/add-two-numbers
In one of the cases, It gives a large number
In one of the cases, It gives a large number
@Anay-208 Leetcode challenge: https://leetcode.com/problems/add-two-numbers
In one of the cases, It gives a large number
That’s not how you are supposed to solve the problem. The problem is there to ask you to implement manual addition of big numbers. Using existing solutions such as BigInt defeats the whole purpose and helps you with nothing
Performing addition of two big integers is a classic problem that every computer science graduate is expected to know how to solve
Anay-208OP
umm got it