c# Correct way to convert string value to decimal

I need convert the any value in format "101.46" to decimal.

string s = "101.46";
decimal value = Convert.ToDecimal(s);

But the decimal final value is always 10146. I need the format of the output value to be 101.46 as decimal

Jon Skeet
people
quotationmark

Convert.ToDecimal will use the currently executing thread's culture - and my guess is that in your case, that's a culture that uses , as the decimal separator rather than .. You just need to specify the right culture - which is often the invariant culture - that's almost always the right culture to use for any strings that are intended to be machine-readable. (You should use the invariant culture for creating such strings too.)

I'd generally recommend using the static Parse methods over Convert.ToXyz (as they're often more capable, and have TryParse options too), so while you could pass the invariant culture to Convert.ToDecimal, I'd use:

decimal value = decimal.Parse(text, CultureInfo.InvariantCulture);

people

See more on this question at Stackoverflow