C# MVC.Net format currency without decimals

前端 未结 3 616
青春惊慌失措
青春惊慌失措 2020-12-20 17:15

I\'m looking for a DataFormatString that will display a float as a currency. But omit the decimal values if they\'re irrelevant (0\'s).

At the moment I\'m using:

3条回答
  •  一个人的身影
    2020-12-20 17:57

    var price = 100.0M;
    var curr = price % 1 == 0 ? price.ToString("C0") : price.ToString("C");
    

    You can put this in an extension method and call it where you need it.

提交回复
热议问题