How to show a decimal corretly formatted in 0.00$

2019-09-09 11:29发布

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条回答
混吃等死
2楼-- · 2019-09-09 12:18

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

查看更多
登录 后发表回答