1

I wrote big mathematical application with a lot of methods. After deploy it on the system with the same decimal symbol options, my application give good result. When I try deploy my application on the OS with other decimal symbol options, my application get crash.

I read about CultureInfo Class (https://docs.microsoft.com/pl-pl/dotnet/api/system.globalization.cultureinfo?view=netframework-4.7.2).

This class can give me information about decimal symbol format in OS and etc.

I can't imagine rewrote all methods for OS with not the same decimal symbol options like OS where application was compiled.

For example. Below line give a crash application on the OS with different Decimal symbol option:

double d = Double.Parse(testString);

Is it possible get solution this problem with not rewrite all methods ?

tk421
  • 5,557
  • 6
  • 23
  • 33
bipocich
  • 13
  • 3
  • 1
    Are you expecting it to always be `.`, or do you want it to change depending on the system running it? – DiplomacyNotWar Dec 28 '18 at 01:56
  • Yes `,` or `.` is going to be problematic based on `Culture` settings. So you either normalize every input to expected form, with `.` or`,` or you can use a general wrapper for `Double.Parse`. Please have a look here https://stackoverflow.com/a/1354980/6517621 – Hasan Emrah Süngü Dec 28 '18 at 02:00
  • You may want double.Parse (string s, IFormatProvider provider) https://docs.microsoft.com/fr-fr/dotnet/api/system.double.parse?view=netframework-4.7.2#System_Double_Parse_System_String_System_IFormatProvider_ – Calimero Dec 28 '18 at 02:00
  • @John, I expect '.' always. – bipocich Dec 28 '18 at 02:03

1 Answers1

0

Since you do not want to change code at all places, you could change default NumberDecimalSeparator somewhere at the beginning.

You need to replace "," with the character you expect your default NumberDecimalSeparator would be

CultureInfo ci = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name);
if (ci.NumberFormat.NumberDecimalSeparator != ".")
{
    ci.NumberFormat.NumberDecimalSeparator = ".";
    Thread.CurrentThread.CurrentCulture = ci;
}

And then, rest of your code should work properly

var str= @"23,6";
var value = decimal.Parse(str);
Anu Viswan
  • 16,800
  • 2
  • 18
  • 44