Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Convert.ChangeType() when conversionType is decimal and input is "40.00"

I mean, I want to convert this:

string a = "40.00"; Convert.ChangeType(a, typeof(decimal)) 

the result is a decimal value of "4000"

the problem is that the convert call is in a very abstract generic method in a xmlToObject Converter. I don't want to add programmatically lot's of different exceptions to convert correctly.

regards Chris

like image 334
masterchris_99 Avatar asked Oct 11 '11 11:10

masterchris_99


1 Answers

The decimal point might not be represented by the period character in your current culture.

In general, when performing culture-invariant conversions, it's best to specify CultureInfo.InvariantCulture as the IFormatProvider argument to the method:

(decimal) Convert.ChangeType(a, typeof(decimal), CultureInfo.InvariantCulture); 
like image 91
Frédéric Hamidi Avatar answered Oct 01 '22 05:10

Frédéric Hamidi