Hacker News new | past | comments | ask | show | jobs | submit login

Why would you not have a parseFloat/float.parse/whatever?

Parsing floating point values correctly is not a trivial amount of work - to the extent that there have any been security vulnerabilities in implementations.




Of course it should be a api but it should be either explicitly requiring a format OR (more likely) default to an invariant format.

That is, if I do this

    float f = 123.456;
    Console.WriteLine(“value is “+f);
The output shouldn’t vary depending on whether it’s run in France or the US (which it does by default).

For parsing, the argument should even be explicitly required I think. So

    float f = Float.Parse(“1,23”,  frenchLocale); 
So that it’s not ever forgotten or the reader has to wonder if the author forgot it.


JavaScript’s numeric parsing is locale independent. People write too much code blindly to make any kind of locale inference anywhere.

Otherwise I agree with you whole heartedly - I have been stung by absurd locale dependent behaviour in the past (windows’ font loading APIs inexplicably translate the font variant in the programmatic api)


They are not arguing about the stdlib including them, but the api they choose. Instead of parseFloat(str) that tries with a system dependant comma or period, have the decimal type be a parameter or have two explicitly named functions.

Same with dates. Don't try lots of formats on an arbitrary string, force the user to specify the expected format somehow.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: