简体   繁体   中英

c++ console application, atof

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM