0

I rounded the loan amount value. But in my model I have defined the Loan Amount as Decimal. In Grid I need to show it as an Integer (need to avoid the last two zero's after the point).

For example: it shows as 22.00 if the data saved is 21.50. In the grid I need to display as 22 only - avoiding the zeros after the point.

Code:

@Html.DisplayFor(modelItem => item.LoanAmount)
Nick
  • 13,238
  • 17
  • 64
  • 100
user2156088
  • 2,350
  • 5
  • 20
  • 24

3 Answers3

0

You can create Helper method e.g. DisplayDecimal

public static class HtmlExtensionsView
{
    public static string DisplayDecimal(this HtmlHelper helper, decimal? input)
    {
        // you can do you rouding math here
    }
}

then in your .cshtml file you can call (referencing extension's namespace) @Html.DisplayDecimal(modelItem => item.LoanAmount)

More information about rounding in C# is available at http://msdn.microsoft.com/en-us/library/3s2d3xkk.aspx

Dejan Dakić
  • 2,418
  • 2
  • 25
  • 39
  • Regards converting an `decimal` to an `int`, check this out: http://stackoverflow.com/questions/501090/how-do-i-convert-a-decimal-to-an-int-in-c – Jason Evans Jun 19 '13 at 08:09
0

In your view you can simply add the following:

@string.Format("{0:#}", modelItem.LoanAmount)
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
0

You can use DisplayFormat attribute to make it work as per your choice.. Just decorate your property with displayformat and provide appropriate format.

[DisplayFormat(DataFormatString = "YOUR_FORMAT")]
public decimal YourProperName {get;set;}
K D
  • 5,889
  • 1
  • 23
  • 35