'How to cast string with comma to double using boost
it is possible to do something like that
std::string str = "127,8";
double x = boost::lexical_cast<double>(str);
I tried it but it throws an exception, do u have a simple way to do such cast using boost ?
Solution 1:[1]
You can use a custom locale that uses , for the decimal point. Code taken and modified from https://stackoverflow.com/a/7277333/4117728
#include <iostream>
#include <boost/lexical_cast.hpp>
class comma_numpunct : public std::numpunct<char>
{
protected:
virtual char do_decimal_point() const
{
return ',';
}
};
int main(int argc, char *argv[]) {
std::string str = "127,8";
std::locale comma_locale(std::locale(), new comma_numpunct());
std::locale::global(comma_locale);
double x = boost::lexical_cast<double>(str);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 463035818_is_not_a_number |
