C# MVC.Net format currency without decimals

前端 未结 3 614
青春惊慌失措
青春惊慌失措 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:49

    I don't think there is a way to do it that explicitly uses currency formatting, but you can use custom formatting using the # character: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

    This should work for U.S. currency:

    [DisplayFormat(DataFormatString = "{0:$#.##}")]
    

    The # character only represents a digit if it's needed.

提交回复
热议问题