.NET 舍入帮助
我有 10 个小数(双精度类型),加起来为 1。我的客户想要在我们的网格中显示一位小数,我尝试使用小数数据类型 Math.Round(Away 和 Even),但我只能无法让数字相加为 1 (0.98)。我的数学技能缺乏,我不知道如何正确地做到这一点。任何帮助都会很棒!
0.429068150 0.222531293
0.091098748 0.062586926
0.053546592 0.047983310
0.031988873 0.024339360
0.022948540 0.013908206
I have 10 fractional numbers (double type) that add up to 1. My client wants to show one decimal place in our grid and I've tried using the decimal data type, Math.Round (Away and Even), but I just can't get the numbers to add up to 1 (.98). My math skills are lacking and I can't figure out how to do this correctly. Any help would be great!
0.429068150 0.222531293
0.091098748 0.062586926
0.053546592 0.047983310
0.031988873 0.024339360
0.022948540 0.013908206
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些数字加起来似乎非常接近原样。
如果我理解正确的话,您想要对一堆数字进行四舍五入以用于显示目的,但仍然能够对它们进行数学计算并获得如果您没有对它们进行四舍五入的话会看到的结果。
因此,为了快速说明问题:1.5 + 2.5 = 4,而 1 + 2 = 3。四舍五入改变了数字,从而改变了结果。
您可以做的是将它们显示在网格中,按照您想要的方式舍入,但在幕后保留数字的高保真(未舍入)版本,并对其进行总计。
The numbers seem to add up to something very close to one as-is.
If I'm understanding you correctly, you want to round a bunch of numbers for display purposes but still be able to do math on them and get the results you would've seen if you had not rounded them.
So to illustrate the problem real quick: 1.5 + 2.5 = 4, whereas 1 + 2 = 3. Rounding changed the numbers and that changed the result.
What you could do is display them in the grid rounded however you want, but keep the high fidelity (unrounded) version of the numbers behind the scenes, and do your totaling on that.
它们加起来为
0.999999998000000
。您可能正在寻找格式字符串。您可以在列的属性中设置它们。 (在不确切知道您使用的是什么的情况下,我无法进一步帮助确定格式字符串的放置位置。)这将打印 1.0。请注意
{0:N1}
。这就是说打印保留一位小数的数字。They add up to
0.999999998000000
. You're probably looking for format strings. You can set them in your column's properties. (Without knowing exactly what you're using, I can't help further with where to place the format string.)That prints 1.0. Notice the
{0:N1}
. That says print the number with one decimal place.