what decimal value got rounded in Razor view?

天涯浪子 提交于 2019-12-23 04:45:10

问题


My model contains a decimal column, I have checked inside Controller action it has decimal values like below:

0.69874
0.78562

Strange though, when they appear on web page, they are rounded and they become 0.70

My model class has this property as:

  public virtual decimal Rate { get; set; }

Inside view code is:

<td>@(Html.DisplayFor(m=>row.Rate))</td>

Can you please guide why it is being round automatically and what I should do to stop this, I want to display what ever is real value.

Much thanks for your guidance and helping me.


回答1:


Try adding a Display attribute on the property in question

[DisplayFormat(DataFormatString = "{0:F3}")]
public virtual decimal Rate { get; set; }

Here are more options for formats http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx



来源:https://stackoverflow.com/questions/19416431/what-decimal-value-got-rounded-in-razor-view

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