Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove $ from .ToString("{0:C}") formatted number

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; }
}
like image 469
Ancient Avatar asked Jul 09 '13 06:07

Ancient


4 Answers

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.

like image 100
Damien_The_Unbeliever Avatar answered Nov 07 '22 08:11

Damien_The_Unbeliever


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
like image 39
Nitin Katiyar Avatar answered Nov 07 '22 07:11

Nitin Katiyar


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.

like image 4
ChrisGheen Avatar answered Nov 07 '22 07:11

ChrisGheen


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

like image 2
MarcinJuraszek Avatar answered Nov 07 '22 09:11

MarcinJuraszek