Best way to convert string to decimal separator "." and "," insensitive way?
I found another way to do it. It looks odd but it works fine for me.
So if you don't know culture of the target system and you don't know which value you will get like 12.33 or 12,33 you can do following
string amount = "12.33";
// or i.e. string amount = "12,33";
var c = System.Threading.Thread.CurrentThread.CurrentCulture;
var s = c.NumberFormat.CurrencyDecimalSeparator;
amount = amount.Replace(",", s);
amount = amount.Replace(".", s);
decimal transactionAmount = Convert.ToDecimal(amount);
You can create a temporary CultureInfo
object to use when you parse.
// get a temporary culture (clone) to modify
var ci = CultureInfo.InvariantCulture.Clone() as CultureInfo;
ci.NumberFormat.NumberDecimalSeparator = ",";
decimal number = decimal.Parse("1,1", ci); // 1.1