title: DisplayFormat for TextBoxFor in MVC
body: <p>I need to round off 4 digit decimal to 2 digits and show in MVC 3 UI</p>
  
  <p><strong>Something like this 58.8964 to 58.90</strong> </p>
  
  <p>Tried following this <a href="https://stackoverflow.com/questions/5080451/how-should-i-use-editorfor-in-mvc-for-a-currency-money-type">How should I use EditorFor() in MVC for a currency/money type?</a> but not working. </p>
  
  <blockquote>
    <p>As i am using TextBoxFor=> i removed ApplyFormatInEditMode here. Even
    i tried with ApplyFormatInEditMode , but nothing works. Still showing
    me 58.8964.</p>
  </blockquote>
  
  <p>MyModelClass</p>
  
  <pre><code> [DisplayFormat(DataFormatString = "{0:F2}")]
   public decimal? TotalAmount { get; set; }
  
   @Html.TextBoxFor(m=&gt;m.TotalAmount)
  </code></pre>
  
  <p><strong><em>How can i achieve this round off?</em></strong></p>
  
  <p><strong>I can't use EditorFor(m=>m.TotalAmount) here, as i need to pass some htmlAttributes</strong></p>
  
  <p>Edit:</p>
  
  <p>After debugging with MVC source code, they internally use  </p>
  
  <pre><code> string valueParameter = Convert.ToString(value, CultureInfo.CurrentCulture);
  </code></pre>
  
  <p>in MvcHtmlString InputHelper() method of InputExtension.cs that takes  <strong>object value</strong> as parameter and converting. They are not using any display format there. How could we fix?</p>
  
  <p>I managed to fix in this way. As i have a custom helper, i can able to manage with the below code</p>
  
  <pre><code> if (!string.IsNullOrEmpty(modelMetaData.DisplayFormatString))
     {
       string formatString = modelMetaData.DisplayFormatString;
       string formattedValue = String.Format(CultureInfo.CurrentCulture, formatString, modelMetaData.Model);
       string name = ExpressionHelper.GetExpressionText(expression);
       string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
         return htmlHelper.TextBox(fullName, formattedValue, htmlAttributes);
     }
     else
     {
         return htmlHelper.TextBoxFor(expression, htmlAttributes);
     }
  </code></pre>