Welcome to MSDN Blogs Sign in | Join | Help

SYSK 107: The Difference Between Decimal.ToString(“C”) and Decimal.ToString(“C2”)

Formatting string “C2” explicitly tells to leave two decimals.  One could also use “C0” (no decimals), “C1”, “C3”, “C4”. Many believe that there is no difference between the format strings “C” and C2”.   While it’s true in the default scenario, it’s only the case since CurrencyDecimalDigits is initialized to 2.  However, in the code below ToString(“C”) results in 4 decimal digits, just like “C4”, not “C2”:

System.Globalization.NumberFormatInfo nfi = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat;

nfi.CurrencyDecimalDigits = 4;

 

decimal x = 5.3428M;          

 

System.Diagnostics.Debug.WriteLine(x.ToString("C2"));

System.Diagnostics.Debug.WriteLine(x.ToString("C"));

 

Published Tuesday, April 18, 2006 5:22 AM by irenak

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker