gpt4 book ai didi

c# - double 值的 string.Format() 给出不准确的结果

转载 作者:太空狗 更新时间:2023-10-30 00:24:36 25 4
gpt4 key购买 nike

我有这个代码:

<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

(在新标签页中打开图片以查看全尺寸图片。)

如果您查看最右侧列的值,您会注意到它们加起来等于表格右下角的 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 也比实际值高。

最佳答案

所以这才是真正的问题 - “总计”应该是原始数字的总和还是截断数字的总和。 (作为旁注,ROUND减少错误但不会消除它。)。如果总数应该是原始数字的总和,那么您可以:

  1. 保留报告并在脚注中说明由于四舍五入,总数可能不完全匹配。
  2. 为“舍入误差”添加一行。
  3. 将舍入误差分配给一个或多个项目(here's 我有一个关于如何做到这一点的问题和论据)

如果总数应该是四舍五入数字的总和,那么这在报告中很容易做到,但它不会与原始数字的总数相匹配(除非你对原始数字进行四舍五入在源代码中)。

关于c# - double 值的 string.Format() 给出不准确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23376079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com