简体   繁体   English

格式十进制为美国货币

[英]Format decimal as US currency

<%= Math.Round(netValue)%>

One example of it's output could be -1243313 它输出的一个例子可能是-1243313

How do I make sure it's formatted as US currency (with the '$', correct commas, etc.)? 如何确保将其格式化为美国货币(使用'$',正确的逗号等)?

Maybe: 也许:

<%=string.Format(CultureInfo.GetCultureInfo(1033), "{0:C}", Math.Round(netValue)) %>

(1033 is the locale id for the 'en-us' culture) (1033是'en-us'文化的区域设置ID)

If the thread culture happens to already be en-US, then you don't need to specify it. 如果线程文化恰好已经是en-US,那么您不需要指定它。

<%= Math.Round(netValue).ToString("C") %> 

Otherwise, to get the culture for the United States, first create a culture object. 否则,要获得美国的文化,首先要创建一个文化对象。

CultureInfo usaCulture = new CultureInfo("en-US");

You can then pass that to the ToString method on the decimal object. 然后,您可以将其传递给小数对象上的ToString方法。

<%= Math.Round(netValue).ToString("C", usaCulture) %> 

You can use format 's currency code C like so : 您可以使用format的货币代码C如下所示:

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

Edit: If internationalization is an issue, you may want to take a look at localization as well. 编辑:如果国际化是一个问题,您可能也想看看本地化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM