Converting string with many leading zeros to decimal

Viewed 105

How can I convert string which looks like this:

0005.47

to decimal value 5.47.

This value is kept in newStringArray[1] so I did this: Convert.ToDecimal(newStringArray[1])

But as a result I got this value : 547

What's the point here?

1 Answers

Your globalization setting thinks that a point is not the decimal separator but the thousands separator

You need to pass the information about this to Convert.ToDecimal. It can be done passing the CultureInfo.InvariantCulture property to inform the converter to use the proper decimal symbol when converting.

string test = "0005.47";
decimal value = Convert.ToDecimal(test, CultureInfo.InvariantCulture);
Related