我有这段代码:
<tbody>
@foreach (var day in user.WorkDays)
{
<tr>
<th>@day.Date.ToString("MM/dd/yy")</th>
<td>
<ul>
@foreach (var note in day.Notes)
{
<li>@note.Text</li>
}
</ul>
</td>
<td>@string.Format("{0:0.00}", Math.Truncate(day.Totals.Regular * 100) / 100)</td>
<td>@string.Format("{0:0.00}", Math.Truncate(day.Totals.Overtime * 100) / 100)</td>
<td>@string.Format("{0:0.00}", Math.Truncate(day.Totals.Doubletime * 100) / 100)</td>
<td>@string.Format("{0:0.00}", Math.Truncate(day.Totals.Sick * 100) / 100)</td>
<td>@string.Format("{0:0.00}", Math.Truncate(day.Totals.Vacation * 100) / 100)</td>
<td>@string.Format("{0:0.00}", Math.Truncate(day.Totals.Holiday * 100) / 100)</td>
<td>@string.Format("{0:0.00}", Math.Truncate(day.Totals.Overall * 100) / 100)</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<th>Totals:</th>
<th></th>
<th>@string.Format("{0:0.00}", Math.Truncate(user.Totals.Regular * 100) / 100)</th>
<th>@string.Format("{0:0.00}", Math.Truncate(user.Totals.Overtime * 100) / 100)</th>
<th>@string.Format("{0:0.00}", Math.Truncate(user.Totals.Doubletime * 100) / 100)</th>
<th>@string.Format("{0:0.00}", Math.Truncate(user.Totals.Sick * 100) / 100)</th>
<th>@string.Format("{0:0.00}", Math.Truncate(user.Totals.Vacation * 100) / 100)</th>
<th>@string.Format("{0:0.00}", Math.Truncate(user.Totals.Holiday * 100) / 100)</th>
<th>@string.Format("{0:0.00}", Math.Truncate(user.Totals.Overall * 100) / 100)</th>
</tr>
</tfoot>
它产生了这个结果:
![enter image description here](https://istack.dev59.com/GcHmr.webp)
如果你看右侧列上的值,你会注意到它们不会加起来等于表格右下角的79.98。我计算它们加起来是79.93。
既然我知道有人会问,是的,79.98是正确的总数。应该加起来的值是不正确的。
我做错了什么?我已经调整了很长时间了,但没有看到任何变化。
编辑:
阅读一些评论后,很明显Math.Truncate()调用并没有帮助。以下是我之前的代码:
<tbody>
@foreach (var day in user.WorkDays)
{
<tr>
<th>@day.Date.ToString("MM/dd/yy")</th>
<td>
<ul>
@foreach (var note in day.Notes)
{
<li>@note.Text</li>
}
</ul>
</td>
<td>@day.Totals.Regular.ToString("0.00")</td>
<td>@day.Totals.Overtime.ToString("0.00")</td>
<td>@day.Totals.Doubletime.ToString("0.00")</td>
<td>@day.Totals.Sick.ToString("0.00")</td>
<td>@day.Totals.Vacation.ToString("0.00")</td>
<td>@day.Totals.Holiday.ToString("0.00")</td>
<td>@day.Totals.Overall.ToString("0.00")</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<th>Totals:</th>
<th></th>
<th>@user.Totals.Regular.ToString("0.00")</th>
<th>@user.Totals.Overtime.ToString("0.00")</th>
<th>@user.Totals.Doubletime.ToString("0.00")</th>
<th>@user.Totals.Sick.ToString("0.00")</th>
<th>@user.Totals.Vacation.ToString("0.00")</th>
<th>@user.Totals.Holiday.ToString("0.00")</th>
<th>@user.Totals.Overall.ToString("0.00")</th>
</tr>
</tfoot>
现在,这些数值加起来甚至达到了
80
。即便如此,这个值仍然比实际值高了.02
。
1.004, 1.004, 1.004
但显示为1.00, 1.00, 1.00
。您显示/手动计算的总和显然是3.00
,但如果您对底层值求和,您将得到3.012
(它将显示为3.01
)。我不确定您在这里想要做什么。也许您应该将每行的值显示为更多的有效数字。 - Chris Sinclairfloat
到decimal
的选项? - crthompson