38

Basically I am formatting numbers like this

@String.Format("{0:C}", Model.Price)

The result is $2,320,000.00

My desired result, however, is 2,320,000.00 just without $, respectively. How can I attain this, while still taking advantage of .nets localization handling?

Edit

and what if i want that change my class so that when ever a person try to get Price , he gets this kind of formatted price . Here is my class

public class Listing : DomainObject
{   
    public decimal Price { get; set; }
    public decimal Commission { get; set; }
}
Ancient
  • 3,007
  • 14
  • 55
  • 104
  • Reason behind my edit is that in my first case i have to change on every place where i use place and in second condition it will automatically done everywhere . – Ancient Jul 09 '13 at 06:27
  • You could create special method as a member of your class.. – rudolf_franek Jul 09 '13 at 06:48

4 Answers4

75

Use N instead of C as your format specifier:

@String.Format("{0:N}", Model.Price)

"C" or "c" Currency Result: A currency value.

123.456 ("C", en-US) -> $123.46

"N" or "n" Number Result: Integral and decimal digits, group separators, and a decimal

1234.567 ("N", en-US) -> 1,234.57

I.e. you were getting a currency symbol because you were asking for one.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • Thanks , this works fine , but what about my second question ? – Ancient Jul 09 '13 at 06:35
  • @user2413182 - doesn't really make sense - if someone is specifying to format a decimal in a string as currency, the currency symbol will be included. Anything else would be deeply surprising, which doesn't make for a good programming experience. – Damien_The_Unbeliever Jul 09 '13 at 06:37
  • 1
    This changes the behavior of negatives though, they are in parenthesis with {0:C} and show negative sign with {0:N}, so you solve one issue, and possibly present another if you wised to have negatives appear as currency just without the currency symbol. – Taylor Brown Jan 06 '17 at 19:10
  • 1
    As I see in practice right now, "N" gets the NumberDecimalSeparator from CultureInfo, while "C" uses the Currency ones. You can see that by yourself using ru-RU – DevOvercome Jul 10 '17 at 00:13
  • This answer could be improved by explaining why using the "N" format specifier in this example results in 2 decimal digits (as opposed to none, or 3, etc.) – rory.ap May 12 '20 at 14:01
6

Rather use {0:F2} as this will not include Comma',' like in {0:C}.

Console.Write("{0:F2}", 2.5); //2.50
Console.Write("{0:F0}", 25);  //25
Nitin Katiyar
  • 143
  • 2
  • 10
4

You'll want to make the Price property a string, and have a backing field of a decimal, then in the getter of your Price property do your string format.

private decimal price;

public String Price 
{
   get { return String.Format("{0:N}", price); }
}

Then you can use @Model.Price for display.

ChrisGheen
  • 984
  • 6
  • 17
2

Set CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol to string.Empty.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263