I want to convert a std::string which I read from a csv file to a float. There are several float representations included like:
0,0728239
6.543.584.399
2,67E-02
These string should all be floats. First I used atof(), but the conversion was wrong:
2,67E-02 -> 2
6.543.584.399 -> 6.543
Then I used boost::lexical_cast<float>(), but when it comes to a float with an exponent included, it throws following exception
`terminate` called after throwing an instance of
`'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >'`
`what()`: bad lexical cast: source type value could not be interpreted as target
Aborted
What is the best way to get all three types of strings converted to a float?
6.543.584.399cannot be valid at the same time as0.0728239.to separate the groups of thousands, and,to separate the whole part of the fractional part." By default, C++ will use theC locale, which uses.to separate the whole part from the fractional part, and doesn't use anything to separate the groups of thousands. There's a "C++ way" of using locales ( www2.research.att.com/~bs/3rd_loc0.html ), but nobody uses it and some compilers don't implement it. The "C way" is pretty straightforward, though.