Decimal Separator Locales

Hey guys,

I'm new to the forum but I searched for this question and didn't find.

I have an application that is used around the world. It uses a MYSQL database in some countries and Oracle in others. The front end is written in C++; I've used resource files for culture conversions in ASP.NET but I'm very very new to C++ and I'm not seeing a way to do that in this language. So I'm hoping someone can help me out with this one.

Suppose I have a number in the database, say, 0.05. When it is displayed on the form in say Belgium it needs to be displayed as "0,05". That part has been taken care of by someone else in the past. But now I've been tasked with going back the other way. So taking the value "0,-5" from a TextBox and parsing it into a double variable to send to the back end database.

Obviously the problem here is that "0,05" can't be parsed because it's not a valid number. Now, this application is massive, and very very complicated. So I need a way to convert numbers with "," for the decimal separator and say " " for the thousands separator and parse it into a double. But I need something that is going to work for the entire application without having to recode everything that parses a number; that would literally take months in this application.

Just for example; here's a sample of the code.
    dDoubleValue = atof("0,05");

Hope you guess can help me out here; like I said I'm new to C++ and I'm stumped.

I'm not very good when it comes to using C style strings (or much of C for that matter) but I put this together to handle your situation.
#include <cstdlib>
#include <cstring>
#include <cstdio>

char *ToFloatString(const char myString[]);

int main() {
   // String to be used for testing
   char testString[] = "0,05";
   // Display Results
   printf("Function Converted String: %.2f", atof(ToFloatString(testString)));
   printf("\natof Converted String: %.2f", atof(testString));
   printf("\nOriginal string: %s", testString);

   return 0;

char *ToFloatString(const char *myString) {
   // Create a pointer to myString
   char *p; strcpy(p, myString);

   // Check to see if the string contains a comma
   if ((p = strchr(p, ',')) != NULL)
      // Change the comma to a period
      *p = '.';

   // Return the modified string
   return p;
Function Converted String: 0.05
atof Converted String: 0.00
Original string: 0,05

I noticed some significant differences. Using C style I/O, it didn't format the string as if it was a floating decimal, but using C++ I/O, it correctly formatted the string. Also, I set up the function to prevent the original string from being changed and that it works nicely together. I didn't get to error test it much, but if you need something changed with the function, just let me know.

Note: I'm not a C programmer but I understand the basics. I hope this is what you wanted and that it has answered your question.
Last edited on
Well that would work for certain situations but not all. For instance, if the number was entered as 5,000 and it is in English locale then it's not going to need to change the "," to a ".". I need a way to convert it depending on the computer's regional settings.
I believe the computer already applies regional settings when using C++. Otherwise, programmers in other nations (specifically countries that use , to denote a decimal) would be forced to write everything with a period instead.

Maybe someone else can elaborate on this, but I believe once compiled, simply changing your locale (to a country that uses a comma to test the code) will fix this.

Edit: I just did a bunch of research on being able to test the system's locale and I found out that the program should automatically be able to tell what locale their using and adjust number formats accordingly. I couldn't effectively change my locale (using English (US)) to test if this is true, but it might be possible that it works. If not, there is a C++ standard header for this #include <locale> that can adjust values correctly (or so it says).

I'd suggest testing sample code on a machine with a different locale (obviously Americans shouldn't be typing 0,05 and Germans shouldn't be typing 0.05) to see if this is the case. If not, there is a way to parse a string to convert it to the appropriate locale.
Last edited on
Topic archived. No new replies allowed.