开发者

Displaying currency in C#

I need to display data 开发者_开发技巧values in US currency format. Meaning 190.8 should display as $190.80. For some reason I cant figure out how to do this. Any advice?


You could explicitly specify the US culture like so:

string.Format(CultureInfo.GetCultureInfo("en-US"), "{0:C}", decimalValue)

The C indicates the default currency format for the specified culture, in this case exactly what you're after. If you want the US currency symbol with a continental European number format (comma instead of period) then your job would be harder of course...


Standard Numeric Format Strings

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

or

double value = 12345.6789;
Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));//CultureInfo.GetCultureInfo("en-US")
// current culture is English (United States):
//       $12,345.68


 decimal d = 190.8M;
 string displayData = d.ToString("c");

If your CurrentCulture is already US there's no need to explicitly supply it.


string usCurrency = (190.8m).ToString("c", CultureInfo.GetCultureInfo("en-US"));


String.Format("${0:n2}", 190.8m);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜