-3

I am unable to understand this:

parseFloat('1/2') == 1 Not Expected
parseFloat(1/2) == 0.5 Expected
parseFloat('0.5') == 0.5 Expected
parseFloat(0.5) == 0.5 Expected

Is it some issue or am I doing something wrong? Also, how to get

parseFloat('1/2') == 0.5
Shubhendu Vaid
  • 567
  • 3
  • 17

4 Answers4

11

As in doc mentioned parseFloat

parseFloat parses its argument, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed.

so 1/2 treated as a string. Not only that - this string does not contain a valid number representation in JavaScript.

Numbers in JavaScript may include -, 0-9, . and +e. / is not a part of it. Therefore - parseFloat parses all the characters that are legal as a number - which in your case is just the 1 part, and ignores rest.

1/2 in JavaScript is not a number, but an expression including 2 numbers and an operator (1 = num, / = operator, 2 = number). What can execute expressions?

You can use eval to calculate fractional form.

console.log(eval('2/3'))

Mind that eval is a dangerous function: using eval on user-input can lead to exploits.

Radagast the Brown
  • 3,156
  • 3
  • 27
  • 40
Durga
  • 15,263
  • 2
  • 28
  • 52
6

parseFloat does not understand the / character as a division nor does it do an eval of the string input.

It simply stops looking when it encounters the character it doesn't understand and returns the correctly parsed first part:

console.log(
  parseFloat("1/2"), // 1
  parseFloat("3/2"), // 3
  parseFloat("1kahsdjfjhksd2") // 1
)

If you do want to evaluate the string "1/2" to the number 0.5, you can use eval. Be careful, because using eval can be a security risk, slow and hard to debug.

console.log(
  eval("1/2")
);
user3297291
  • 22,592
  • 4
  • 29
  • 45
  • Agreed, but if I want to use a polynomial as a string is it not possible. – Shubhendu Vaid Apr 18 '18 at 13:06
  • 1
    You can use `eval("1/2")`, but you should research the [pitfalls of using `eval`](https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea) and decide for yourself if it's worth it. – user3297291 Apr 18 '18 at 13:07
1

Your parsing a string that will be converted to 1. If your string was only numbers (e.g. "0.5") them they would be converted correctly, but as it includes the '/', the automatic type conversion will not occur and it will remain as a string. When using numbers the expected behavior occurs, that is:

parseFloat(1/2) === 0.5 // true
Guilherme Lemmi
  • 3,271
  • 7
  • 30
  • 30
  • This is true, but if you're already working with numbers there's no reason to call `parseFloat()` at all. In fact doing that will convert the number to a string first and then convert it back. – Pointy Apr 18 '18 at 12:58
1

Not 100% sure. but if you play around with parseFloat a bit you will see that it tries to convert every number it finds to a float, but stops as soon as there is a unexpected value so :

parseFloat('1/asdf') == 1

but

parseFloat('0.5') == 0.5

So parse float does not calculate for you, but just parses every number it finds, until there is something non numerical.

glm9637
  • 894
  • 9
  • 20