我的十进制值是连接而不是加在一起

时间:2015-08-23 21:00:46

标签: asp.net-mvc decimal addition

您好我想为列表中的每个项目执行总计。我为每个项目计算了一个值calculateValue =(价格x数量)。但是现在我想把那个calculateValue添加到overallTotal并在底部显示这个变量。

我的错误是,如果我有例如totalTotal = 50.00 + 10.00 + 20.00

当我显示@overallTotal时,它将显示为“50.0010.0020.00”而不是80.00。我无法转换为int,否则我将丢失小数点

这是我的表格的代码

                <tbody>
                    <tr>
                        <th><i class="icon_pin_alt"></i> Item Description</th>
                        <th><i class="icon_pin_alt"></i> Quantity</th>
                        <th><i class="icon_calendar"></i> Price</th>
                        <th></th>
                    </tr>

                    @foreach (var item in Model.ITEM_ORDER)
                    {
                        decimal computedValue = item.item_order_quantity * item.ITEM.item_price;
                        decimal overallTotal = 0;
                        overallTotal= 0+(overallTotal+ computedValue);
                        <tr style="background-color:white; border-style:hidden">
                            <td style="width:auto">
                                @Html.DisplayFor(modelItem => item.ITEM.item_brand)
                                @Html.DisplayFor(modelItem => item.ITEM.item_description)
                            </td>
                            <td style="width:auto">
                                @Html.DisplayFor(modelItem => item.item_order_quantity)
                            </td>
                            <td style="width:auto">

                               R @computedValue

                            </td>
                            <td>
                                <div class="btn-group; background-color:white">
                                    @Html.ActionLink("View", "Details", new { id = item.OrderID }) |
                                    @Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |

                                    @Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
                                </div>
                            </td>
                        </tr>
                    <tr>R @overallTotal</tr>
                    }


                </tbody>
            </table>

1 个答案:

答案 0 :(得分:0)

阅读完ASP文档后,我意识到你需要用&#39; m&#39;字符将它们视为小数,如果不是它们是双打。这应该有效:

// Before the loop
@{ decimal overallTotal = 0M; }
// for...
decimal computedValue = (decimal) item.item_order_quantity * (decimal) item.ITEM.item_price;    
overallTotal += computedValue;
// end for

如果 item.item_order_quantity 已经是小数,请删除转换。对于 item.ITEM.item_price

也是如此