Link to home
Start Free TrialLog in
Avatar of rp
rpFlag for Portugal

asked on

Decimal and Thousands separator

Hi,
I have a problem with an application, I think it has to do with the regional settings.
I have a server with DB Sql Server where the regional settings for numeric formatting are:

Decimal separator (,)
Thousands separator (.)

Some workstations that access via TerminalServices to the server are formatted with the regional settings above, but others have other regional settings such as:

Decimal separator (.)
Thousands separator (,)

Apparently if the workstation is with the same settings of number of the server the calculations are correct, however if they have with the definitions of the 2º exam, in practice the calculations are incorrect. For example. 23000 (twenty three thousand) appears like 23,000 (twenty three)

There will be a way to solve this problem.

best regards
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Just pass the underlying number instead of a string.  Any formatting should only be for display purposes.
Your code could always force the culture to be that of the server:

System.Globalization.CultureInfo.CurrentCulture = new System.Globalization.CultureInfo("de-DE");

Open in new window


For a list of available culture names scroll down towards the bottom of this list:
https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.110).aspx
if you want to handle the separators yourself, you could get the locale settings by WINAPI function GetLocaleInfo

    char szDefDec[4] = {0};
    GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONDECIMALSEP, szDefDec, sizeof(szDefDec));
    char szDefGrp[4] = {0};
    GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHOUSANDSEP, szDefGrp, sizeof(szDefGrp));

Open in new window


the above is c code which also could be used (with little changes) in vb or c#.

for example you could remove the thousands group separator - if any - from string and replace decimal separator character by a period character. then you should be able to using a toDouble conversion without issues.

note, if you don't want to bother with locale seperators at all and have some information which range the numbers are in and how many digits the decimal fraction is, you could do a reverse loop on the string which might match the following pattern "##'###,##". then you would remove the ' and replace the comma by a period in order to get a valid string number that can be converted to double. you even could use a regular expression for this.

Sara
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial