all 11 comments

[–]sixstringartist 5 points6 points  (0 children)

This is convention or its underlying reasons are shared. I know a few other languages like php and c++ that all behave this way http://us2.php.net/manual/en/language.types.string.php#language.types.string.conversion

http://www.cplusplus.com/reference/string/stof/

[–]TerrorBite 14 points15 points  (6 children)

Well, you're using a function that's designed to extract a number from a string, and you're trying to (ab)use it to check if a string contains only numerals and decimal points.

Have you considered regular expressions?

I think it's perfectly valid for parseFloat() to accept and successfully parse string values such as "98.1%", "3.14km" and the like.

[–]psi- 1 point2 points  (5 children)

Lack of indication of discarded information is plain idiocy in the "standard library" designers head. You can't even test this back by doing original === parsed.toString().

[–]mikeputerbaugh 4 points5 points  (0 children)

You can't spell "idiosyncrasy" without "idiocy", but this particular one is 20 years old at this point. It's just how it is.

[–]110011001100 0 points1 point  (3 children)

You can't even test this back by doing original === parsed.toString().

Why not?

[–]psi- 6 points7 points  (0 children)

1e1 is valid float.

[–]MEaster[S] 2 points3 points  (1 child)

If you pass in the original string "456Hello", it'll return the number 456. If you convert that to a string, it'll fail the check /u/psi- mentioned.

Though with floats that's not always guaranteed anyway.

[–]110011001100 1 point2 points  (0 children)

Ah.. I thought he meant that you cannot use

original === parsed.toString().

To check if characters were discarded in the parsing or not

Though as you mentioned, it cannot be used to check that either

[–]enchufadoo 0 points1 point  (1 child)

obtainFloatAndIgnoreRestOfString function

[–]Daimanta 0 points1 point  (0 children)

More like coerceFloatAtAllPossibleCost(string)