-1

I want to change the windows culture settings programmatically since when using my program I need to parse the dot "." as the decimal number and windows has by default set the ",". So I'm looking for this.

I already tried by changing the current app culture but when parsing data from db it still gets in trouble because it uses windows configuration.

Emilien
  • 19
  • 4
Delynith
  • 114
  • 8
  • 4
    This seems like **the worst** approach I could imagine. Is it stored as a string in the database? For example: "123.05" needs to be parsed to a decimal? – Austin T French Aug 22 '19 at 13:35
  • yes, but don't tell me to make a query and change all of it, because i'm not allowed to, basically it's something they asked me at work to do – Delynith Aug 22 '19 at 13:36
  • 1
    So you want to change the culture of not even your machine, but the database server, to be custom locale with subtle differences to the underlying locale. What could go wrong. – GSerg Aug 22 '19 at 13:42
  • You could threat dot/comma equally, see [this question](https://stackoverflow.com/q/11560465/1997232) – Sinatr Aug 22 '19 at 13:42
  • No, I just want to change the local machine language since when receiving the data from db it uses the local culture config and parses everything – Delynith Aug 22 '19 at 13:46
  • Thank you, I will take a look at it, but anyways its not what I'm looking for @Sinatr – Delynith Aug 22 '19 at 13:47
  • 2
    On the C# level, pass the required locale to the [respective overload of `double.Parse`](https://learn.microsoft.com/en-us/dotnet/api/system.double.parse?view=netframework-4.8#System_Double_Parse_System_String_System_IFormatProvider_). On the database level (I assume SQL Server), provide an explicit culture to [`parse`](https://learn.microsoft.com/en-us/sql/t-sql/functions/parse-transact-sql?view=sql-server-2017) (`parse('20.5' as float using 'en-us')`). [Do not](https://devblogs.microsoft.com/oldnewthing/20081211-00/?p=19873) change locale settings on either machine. – GSerg Aug 22 '19 at 13:53

1 Answers1

1

I'll emphasize, do it better.

This can be pretty easily solved, and a quick example:

public static decimal GetInvariantDecimal(string internationDecimalString)
{
    var looksUnAmerican = Regex.IsMatch(internationDecimalString, @"(\d+,\d{2}\b)|(\d+\.\d+,\d{0,2})|(\d+\.\d{3})");
    Console.WriteLine(looksUnAmerican);
    return looksUnAmerican ? 
        Decimal.Parse(internationDecimalString, NumberStyles.Currency, CultureInfo.GetCultureInfo("tr-TR")) :
        Decimal.Parse(internationDecimalString,  CultureInfo.InvariantCulture);
}

public static void Main()
{
    var american = "123.55";
    var international = "234,55";       

    Console.WriteLine(GetInvariantDecimal(american));
    Console.WriteLine(GetInvariantDecimal(international));
}

It will give you a standard decimal for the correct environment, and you would use this every time you work with the string from the database. (of course, it doesn't currently handle something like "1,234.01"...

But output:

123.55
234.55

And one more bit, changing the users machines culture will likely cause all sorts of bugs in who knows what...

Austin T French
  • 5,022
  • 1
  • 22
  • 40
  • 2
    Except that in countries with a `,` as decimal separator, the `.` is used for thousands... so "123.45" is 123450. It's not one-size-fits-all, you need to know the culture of the input and the culture of the output to do a proper translation. – nvoigt Aug 22 '19 at 14:04
  • @nvoigt agreed, and I called that out. Perhaps not explicitly enough that it will fail with valid formats that weren't explicitly mentioned. This was meant to move the OP in a better direction – Austin T French Aug 22 '19 at 16:18