Search This Blog

Monday, January 10, 2011

Show number in Currency format in C#

When building a string for output to a web page, it’s useful to format any currency value in a human-friendly money format. This is extremely easy in C#.

The system format string works like this: {0:C}
For example, the following code example


decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html); 



Numeric Format Specifiers
Specifier Description Example C#
c Currency; specify the number of decimal places $12,345.00 string.Format("Currency: {0:c}", iNbr)
d Whole numbers; specifies the minimum number of digits - zeroes will be used to pad the result 12345 string.Format("Whole: {0:d}", iNbr)
e Scientific notation; specifies the number of decimal places 1.2345e+004 string.Format("Exponential: {0:e}", iNbr)
f Fixed-point; specifies the number of decimal places 12345.00 string.Format("Fixed: {0:f3}", iNbr)
n Fixed-point with comma separators; specifies the number of decimal places 12,345.00 string.Format("Fixed formatted: {0:n3}", iNbr)
p percentage; specifies the number of decimal places 1,234,500.00% string.Format("Percentage: {0:p2}", iNbr)
x Hexadecimal 3039 string.Format("Hexadecimal: {0:x}", iNbr)

1 comment:

Note: Only a member of this blog may post a comment.