Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format decimal as US currency

Tags:

c#

.net

decimal

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

like image 833
slandau Avatar asked Mar 03 '11 21:03

slandau


2 Answers

Maybe:

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

(1033 is the locale id for the 'en-us' culture)

like image 176
Simon Mourier Avatar answered Sep 23 '22 09:09

Simon Mourier


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) %> 
like image 37
Jeffrey L Whitledge Avatar answered Sep 21 '22 09:09

Jeffrey L Whitledge