MateuszRek
MateuszRek

Reputation: 59

Converting string with many leading zeros to decimal

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?

Upvotes: 3

Views: 298

Answers (1)

Steve
Steve

Reputation: 216313

Your culture settings think that a point is not the decimal separator but the thousands separator.

You need to pass the information about the culture you want to use 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);

Upvotes: 7

Related Questions