How to show a decimal corretly formatted in 0.00$ format?

末鹿安然 提交于 2019-12-12 03:08:21

问题


I have a view model that show a product.. but the price is in decimal format and i dont want to format it in the view, it should be automaticly formatted using the viewmodel. I know there is a property displayformat.. is there anyone that know to to render the decimal in the view in the formatted form such 0.00$ ?


回答1:


you can use DisplayFormat attribute like following to show the formatted result

[DisplayFormat(DataFormatString = "{0:###.###$}")]
public decimal myVal { get; set; }

Then myVal(lets value is 123.222), will be displayed as 123.222$

More about formatting strings, http://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx



来源:https://stackoverflow.com/questions/8870931/how-to-show-a-decimal-corretly-formatted-in-0-00-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!