Domanda

My problem is the following. I try to convert a string into double. In this way:

string str = "1.1";
double d = atof(str.c_str());

But this don't work, it simply return 1;

But if I try:

string str = "1,1";
double d = atof(str.c_str());

it return 1.1.

That is really weird. Seems it can only understand the number if I write a "," but return as a ".".

Any idea how could I solve this to convert "1.1" as well?

È stato utile?

Soluzione

The function is locale aware, so it will parse the number according to your current locale settings.

Since atof is part of C library, you have to use C library to change the settings. Check out clocale.

Also have a look at C++ locale, which should be used if you use C++ features (string, istringstream) to parse the data. You can imbue the locale to the stream so that you don't modify the global locale as in the case of C.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top