2

So I have a development machine which is a dutch machine and a english server. Now, I have values in the database like 1.99 (string) that I need to convert to a decimal. When I do decimal.Parse("1.99") I get 199.00. This is (probably) because my machine uses Dutch settings and the delimiter for a decimal is a , (comma). Now, how can I set it globaly to always use a .(dot) as the delimiter, regardless of the culture that is beeing requested nor the machines local settings?

I'm using a .Net Core Web Api

NLAnaconda
  • 1,534
  • 3
  • 14
  • 38
  • Check this documentation: https://learn.microsoft.com/en-us/troubleshoot/aspnet/set-current-culture – Cihan Yakar Jul 19 '20 at 19:42
  • Does this answer your question? [Set CultureInfo in Asp.net Core to have a . as CurrencyDecimalSeparator instead of ,](https://stackoverflow.com/questions/40442444/set-cultureinfo-in-asp-net-core-to-have-a-as-currencydecimalseparator-instead) – Pac0 Jul 19 '20 at 19:56

2 Answers2

2

You could use CultureInfo.InvariantCulture that uses . as decimal separator

var number = decimal.Parse("1.99", CultureInfo.InvariantCulture);

If you don't want to set the culture in every parse call, you could create an extension method

public static class StringExtensions
{
    public static decimal ToDecimalInvariant(this string value)
    {
        return decimal.Parse(value, CultureInfo.InvariantCulture);
    }
}

And then use it like this

var number = myString.ToDecimalInvariant();
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
0

I had a similar problem. I have a wpf app, and in the main window viewmodel in the constructor I have this statement:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-Us"); 

I know this might have other (perhaps unwanted) effects on dates and other things but it works for my simple use. I would guess that if you need it other threads you would have to do it for each thread that you start up.

Erik Thysell
  • 1,160
  • 1
  • 14
  • 31