How can I format decimal property to currency?

前端 未结 8 1504
眼角桃花
眼角桃花 2020-12-02 21:50

I want to format a decimal value as a currency value.

How can I do this?

8条回答
  •  隐瞒了意图╮
    2020-12-02 22:37

    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)

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

提交回复
热议问题