Using DataFormatString in the DisplayFormat attributehttp://forums.asp.net/t/1798539.aspx/1?Using+DataFormatString+in+the+DisplayFormat+attributeTue, 01 May 2012 13:13:19 -040017985394958547http://forums.asp.net/p/1798539/4958547.aspx/1?Using+DataFormatString+in+the+DisplayFormat+attributeUsing DataFormatString in the DisplayFormat attribute <p>Hi,</p> <p>I have the following model in MVC3:</p> <pre class="prettyprint">public class ClientFormModel { ... [DisplayFormat(DataFormatString = &quot;{0:n2}&quot;, ApplyFormatInEditMode = true)] public decimal? Salary { get; set; } ... }</pre> <p>As you can see I'm trying to display a decimal with 2 decimal places. However, my view is showing 4 decimal places. View looks as follows:</p> <pre class="prettyprint">@Html.TextBoxFor(m =&gt; m.Salary, new { @class = "Textbox" })</pre> <p><br> Can someone explain how to use DataFormatString properly? Thanks.</p> 2012-04-30T15:50:45-04:004959922http://forums.asp.net/p/1798539/4959922.aspx/1?Re+Using+DataFormatString+in+the+DisplayFormat+attributeRe: Using DataFormatString in the DisplayFormat attribute <p>Using the EditorFor HtmlHelper may solve your issue:</p> <p>[DisplayFormat(DataFormatString = &quot;{0:0.00}&quot;, ApplyFormatInEditMode=true)]<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public decimal? Salary<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>@Html.EditorFor(m=&gt;m.Salary)</p> 2012-05-01T13:13:19-04:00