views:

535

answers:

3

Hi what is the best way to format a decimal amount to string for ui display in the correct culture info?

Many Thanks

+2  A: 

use:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
NetSide
Which will only work if the CurrentCulture is en-US ...
Unsliced
so what? He is not asking for dynamic cultureInfo.
NetSide
+2  A: 

Add a format to the ToString: myDecimal.ToString("#.00") or myDecimal.ToString("C"). See http://msdn.microsoft.com/en-us/library/427bttx3.aspx

Hans Kesting
nHibernate property was Decimal? therefoer the format option was not available on toString method. Now creating decimal variable and using your answer and works a treat. cheers!
c00ke
if it's a Nullable<decimal> then you could either user the .Value property or cast to decimal (if you are sure it's not null)
Hans Kesting
+1  A: 

Why not decimalVar.ToString("F2", CultureInfo.CurrentCulture);. For format strings (the "F2" part) and what they do, see Standard Numeric Format Strings and Custom Numeric Format Strings

Philip Rieck