How to show a decimal corretly formatted in 0.00$

2019-09-09 11:39发布

问题:

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