Sunday, May 28, 2006

How-To: Convert Euro correctly ... [string.ToDecimal()]

I don't really understand why this happens just with Euro, but fact is when I use string.ToDecimal() and my Cultures are formatted for a European country, then it's not converting correctly in manner of:

expected value: 1.534,40
received value: 153440.00

its a small difference, at least to me :-)

The CurrentUICulture contains an NumberFormat which we can append to convert:
System.Globalization.NumberFormatInfo nfUs = Thread.CurrentThread.CurrentUICulture.NumberFormat;

Place attention that first i use "System.Globalization.InvariantCulture" and just on the ToString i apply "c" and my needed culture.

string formattedNumber = Convert.ToDecimal(string.Concat(formating,".",formatingCents), System.Globalization.InvariantCulture).ToString("c", nfUs);

Am I the only one who faced off with this problem?

No comments:

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge