In javascript parsing a string gives me huge number, why? [duplicate] - javascript

This question already has answers here:
How to convert a String containing Scientific Notation to correct Javascript number format
(8 answers)
Closed 5 years ago.
I have a string in response "2.222222522879E12",
When I do
parseFloat("2.222222522879E12")
2222222522879
But the expected value should be 2.22,
Whats happening ?
And how can I fix it

You're going to get 2.222222522879* 10^12 with that expression as E means exponent, or to the power of
As per your edit asking how to fix:
use the Number.prototype.toPrecision() function. Here is the documentation

Related

Why does a number have to be encapsulated in an expression containing that number before methods can be applied to it? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 7 months ago.
1.toString() gives us Uncaught SyntaxError: Invalid or unexpected token.
One the other hand, (1).toString() returns '1'.
Considering that typeof 1 === typeof (1) (both are of type number), why is this the case?
In JS number literals might be both integers and floats, so probably after finding . after the number translator expects to parse a float, like 1.123.

Javascript converting string to number and formatting [duplicate]

This question already has answers here:
How to convert string into float in JavaScript?
(9 answers)
Closed 1 year ago.
So I'm getting this string '41803.96000000'
what i want to do is convert this to a number and it should be in the
right format such as this example '41.96000000'
for anyone wondering this is the current bitcoin price which I'm getting from a binance WebSocket
You can use parseFloat('41803.96000000')

Why the small number string is greater than big number string [duplicate]

This question already has answers here:
Javascript compare numbers as strings
(2 answers)
Closed 2 years ago.
When the smaller number string when compared with the larger number string - the result is true.
How is it possible?
document.write(`Why the result of '2'>'10' is ${'2'>'10'}`)
Any expert here?
You're comparing strings, so a lexical comparison is performed instead of a numerical comparison.
Lexically, 2 comes after 1.

How to turn strings into boolean expressions? [duplicate]

This question already has answers here:
JavaScript -- write a function that can solve a math expression (without eval)
(2 answers)
Closed 3 years ago.
I'm working on a problem generator that spits out phrases like "!(A && B) || (A || B)", but it does so in a string (I have a function that spits this out in string form). How would I convert this string expression into a same boolean expression in javascript? I've tried to use JSON.parse() but it keeps giving me an error.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval should be the function you're looking for, however read the note about the huge security risk!

why does 1..toString() work and return "1" but 1.toSring() cause an syntax error? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 6 years ago.
I know 1 is not an object, but when I type 1..toString(), it returns "1" in the console. Why is that?
Because the JavaScript parser assumes that 1. must be followed only by one or more digits to represent a float number. Using parentheses works: (1).toString().
Because it is interpreting 1. as the number. When you have 1.toString(), it is the same as saying (1.)toString(). Therefore 1..toString() is the same as (1.).toString()
The reason why the below works is:
1..toString()
The 1.. is considered as a floating point number. The console expects something like:
1.0
1.5
Or something. If you are giving something like:
1.toString();
The above is not a valid number. That's the reason. So to make the above work, you need a parenthesis to tell that the number is completed:
(1).toString();

Categories