简体   繁体   English

如何将十进制属性格式化为货币?

[英]How can I format decimal property to currency?

I want to format a decimal value as a currency value.我想将小数值格式化为货币值。

How can I do this?我怎样才能做到这一点?

Properties can return anything they want to, but it's going to need to return the correct type.属性可以返回他们想要的任何东西,但它需要返回正确的类型。

private decimal _amount;

public string FormattedAmount
{
    get { return string.Format("{0:C}", _amount); }
}

Question was asked... what if it was a nullable decimal.有人问问题......如果它是一个可为空的小数怎么办。

private decimal? _amount;

public string FormattedAmount
{
    get
    {
         return _amount == null ? "null" : string.Format("{0:C}", _amount.Value);
    }
}  

Below would also work, but you cannot put in the getter of a decimal property.下面也可以,但是您不能放入小数属性的吸气剂。 The getter of a decimal property can only return a decimal, for which formatting does not apply. decimal 属性的 getter 只能返回小数,格式不适用。

decimal moneyvalue = 1921.39m; 
string currencyValue = moneyvalue.ToString("C");

Try this;试试这个;

  string.Format(new CultureInfo("en-SG", false), "{0:c0}", 123423.083234);

It will convert 123423.083234 to $1,23,423 format.它将 123423.083234 转换为 $1,23,423 格式。

You can create an extension method.您可以创建扩展方法。 I find this to be a good practice as you may need to lock down a currency display regardless of the browser setting.我发现这是一个很好的做法,因为无论浏览器设置如何,您都可能需要锁定货币显示。 For instance you may want to display $5,000.00 always instead of 5 000,00 $ (#CanadaProblems)例如,您可能希望始终显示 $5,000.00 而不是 5,000,00 $ (#CanadaProblems)

public static class DecimalExtensions
{
    public static string ToCurrency(this decimal decimalValue)
    {
        return $"{decimalValue:C}";
    }
}

You can use String.Format, see the code [via How-to Geek ]:您可以使用 String.Format,请参阅代码 [通过How-to Geek ]:

decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
// Output: $1,921.39

See also:也可以看看:

You can now use string interpolation and expression bodied properties in C# 6.您现在可以在 C# 6 中使用字符串插值和表达式主体属性。

private decimal _amount;

public string FormattedAmount => $"{_amount:C}";

Your returned format will be limited by the return type you declare.您返回的格式将受限于您声明的返回类型。 So yes, you can declare the property as a string and return the formatted value of something.所以是的,您可以将属性声明为字符串并返回某些东西的格式化值。 In the "get" you can put whatever data retrieval code you need.在“获取”中,您可以放置您需要的任何数据检索代码。 So if you need to access some numeric value, simply put your return statement as:因此,如果您需要访问一些数值,只需将您的 return 语句写成:

    private decimal _myDecimalValue = 15.78m;
    public string MyFormattedValue
    {
        get { return _myDecimalValue.ToString("c"); }
        private set;  //makes this a 'read only' property.
    }

A decimal type can not contain formatting information.十进制类型不能包含格式信息。 You can create another property, say FormattedProperty of a string type that does what you want.您可以创建另一个属性,例如FormattedProperty的字符串类型,它可以执行您想要的操作。

In My case, I have to convert it into decimal as above mentioned I used to string Format method and then parse it to decimal and it works fine for me here is the example.在我的例子中,我必须像上面提到的那样将它转换成十进制,我曾经使用字符串格式方法,然后将它解析为十进制,它对我来说很好,这里是示例。

public decimal Amount;公共小数金额;

public decimal_amount;公共 decimal_amount;

So I assigned values like this:所以我分配了这样的值:

Amount = decimal.Parse(String.Format("{0:C}", _amount))金额 = decimal.Parse(String.Format("{0:C}", _amount))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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