最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why is parseFloat('12') == 1 in Javascript - Stack Overflow

programmeradmin0浏览0评论

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

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
Share Improve this question asked Apr 18, 2018 at 12:52 Shubhendu VaidShubhendu Vaid 5673 silver badges18 bronze badges 3
  • 3 '1/2' is a string – Durga Commented Apr 18, 2018 at 12:53
  • 3 parseFloat is not eval. – Nina Scholz Commented Apr 18, 2018 at 12:55
  • 1/2 is not a float, it's a mathematical operation. – deceze Commented Apr 18, 2018 at 12:55
Add a ment  | 

4 Answers 4

Reset to default 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.

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")
);

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.

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
发布评论

评论列表(0)

  1. 暂无评论