简体   繁体   中英

Format decimal as US currency

<%= Math.Round(netValue)%>

One example of it's output could be -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)

If the thread culture happens to already be en-US, then you don't need to specify it.

<%= 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.

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

You can use format 's currency code C like so :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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