atof and locale

Posted on 2011-10-17
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

    Accepted Solution

    OK, I found the soultion.
    You need to add:
    LVL 1

    Expert Comment

    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:

    Author Comment

    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

    As I wrote, this soves most of the problem.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    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.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now