1

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,380
  • 2
  • 11
  • 33
  • Check this documentation: https://docs.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

1 Answers1

1

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
  • 65,896
  • 14
  • 126
  • 152