atof and locale

Posted on 2011-10-17
Medium Priority
Last Modified: 2012-08-13

I changed the locale in my control-pannel to French so the number "123,456.50" should be displayed as "123 456,50" The locale settings
Now I put in my C++ program:
double f=atof("123 456,50").
So I should get it, but I get 126
Also, I did
double f=123456.50;
and sprintf(str,"%.02f",f);
I see in str (123456.50)  and not (123456,50 as I expected).

Question by:VapiSoft
  • 3

Accepted Solution

VapiSoft earned 0 total points
ID: 36979662
OK, I found the soultion.
You need to add:

Expert Comment

ID: 36979725
Use the GetNumberFormat() to format your number string.

The following shows how the GetNumberFormat API is used to
format a given number string for the current user locale
(using the default settings for number formatting that the user has defined):

GetNumberFormat(LOCALE_USER_DEFAULT,  // locale (current user locale)
                               0,                                            // options
                               TEXT("1234567890.12345"), // input string (see MSDN for legal chars)
                               NULL,                                     // formatting information
                               g_szTemp,                             // formatted string buffer
                              MAX_STR);                             // size of buffer

(Taken from: http://msdn.microsoft.com/en-us/goglobal/bb688127.aspx)

Author Comment

ID: 36979804
This does not do what I want.
What I need is to get the float number from a string that is formatted using the current locale.

I found the solution:
You need to add
and then atof("123456,50) will return the right number.
BUT, it does not reconize the sapce, so "123 456,50) will return 123.
I can overcome this problem (by converting any number to a number without spaces).

Author Closing Comment

ID: 37010518
As I wrote, this soves most of the problem.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question