1

I have a table in my view with columns of numerical data. In the footer of my table I have a row containing the totals of each column. The totals are adding up correctly, but I want the vales in them to be rounded to 2 decimal places, eg instead of 47.4386, I want it to display 47.44.

Sample code:

<table class="display" id="dailyreporttable">


    @foreach (var item in Model)
{
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.DailyReportDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Estate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.BettingShop)
            </td>
            <td class="alignRight">
                @Html.DisplayFor(modelItem => item.ShopBalance)
            </td>
            <td class="alignRight">
                @Html.DisplayFor(modelItem => item.TotalCashIn)
            </td>
 }

    <tfoot>

        <tr>
            <td>Total</td>
            <td></td>
            <td></td>
            <td class="alignRight">@Model.Sum(item => item.ShopBalance)</td>
            <td class="alignRight">@Model.Sum(item => item.TotalCashIn)</td>
       </tr>
    </tfoot>
Kevin
  • 198
  • 3
  • 6
  • 17

3 Answers3

2

Try this:

 <td class="alignRight">@Model.Sum(item => item.ShopBalance).ToString("N2")</td>
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71
NCC-1701-M
  • 261
  • 1
  • 7
  • Yeah I tried that but I get the error: "Method ToString has 0 parameter(s) but is invoked with 1 argument(s)" – Kevin Jun 04 '14 at 14:16
  • 2
    Is it possible, that item.ShopBalance and TotalCashIn are not numeric? What type are they? In my example it works fine. – NCC-1701-M Jun 04 '14 at 14:20
  • They're both of type decimal – Kevin Jun 04 '14 at 14:26
  • 1
    But decimal has an overloaded ToString Method. http://msdn.microsoft.com/de-de/library/fzeeb5cd(v=vs.110).aspx There must be another error. What type is you model? – NCC-1701-M Jun 04 '14 at 14:52
  • Suppose @Kevin made some mistake in the model – Artiom Jun 05 '14 at 13:28
  • Its actually a nullable decimal, if that makes any difference? eg public Nullable ShopBalance { get; set; } – Kevin Jun 10 '14 at 08:09
1

Got it in the end, the values were actually nullable decimals so I had to do:

 <td class="alignRight">@Model.Sum(item => item.ShopBalance).Value.ToString("N2")</td>
Kevin
  • 198
  • 3
  • 6
  • 17
0

Try:

<td class="alignRight">@Model.Sum(Decimal.Round(item => item.ShopBalance), 2)</td>
JB06
  • 1,881
  • 14
  • 28