C# 从 .ToString("{0:C}") 格式的数字中删除 $

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17541307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:47:51  来源:igfitidea点击:

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

c#string-formatting

提问by Ancient

Basically I am formatting numbers like this

基本上我是这样格式化数字的

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

The result is $2,320,000.00

结果是 $2,320,000.00

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

然而,我想要的结果是分别2,320,000.00没有$。我怎样才能做到这一点,同时仍然利用 .nets 本地化处理?

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

如果我想要改变我的班级,以便当有人试图获得时Price,他会得到这种格式化的价格怎么办?这是我的课

public class Listing : DomainObject
{   
    public decimal Price { get; set; }
    public decimal Commission { get; set; }
}

采纳答案by Damien_The_Unbeliever

Use Ninstead of Cas your format specifier:

使用N而不是C作为您的格式说明符

@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

“C”或“c”货币结果:货币值。

123.456(“C”,美国)-> 123.46 美元

"N" 或 "n" 数字结果:整数和十进制数字、组分隔符和小数

1234.567(“N”,美国)-> 1,234.57

I.e. you were getting a currency symbol because you were askingfor one.

即你得到一个货币符号,因为你一个。

回答by ChrisGheen

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.

您需要将 Price 属性设为字符串,并有一个十进制的支持字段,然后在 Price 属性的 getter 中执行您的字符串格式。

private decimal price;

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

Then you can use @Model.Pricefor display.

然后就可以 @Model.Price用于展示了。

回答by Nitin Katiyar

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

而是使用,{0:F2}因为它不包括逗号',',就像在 {0:C} 中一样。

Console.Write("{0:F2}", 2.5); //2.50
Console.Write("{0:F0}", 25);  //25