如何舍入从数据集中存储的值 [英] How do you round up the values stored from dataset
问题描述
大家好,
我试图将从数据集中获得的值的小数点后一位取整.
我尝试使用Math.round,但无法正常工作.
当前小数点后的位数是17,我想将其四舍五入到7位.
那是我可以用来对数据集中的数组中存储的值进行四舍五入的命令吗?
我的子序列计算需要舍入值,因此我必须进行舍入.
谢谢! :)
Hello everyone,
I am trying to round up the decimal places of the values gotten from a dataset.
I have tried to use Math.round but it could not work.
Currently the decimal places is 17 and I want to round it up to 7 places.
Is that some command I could use to round up the values stored in a array from the dataset?
My subsequence calculation requires the round up values to do so I have to do the rounding up.
Thank you! :)
推荐答案
圆形通常可以正常工作;它对我有用:
Round normally works fine; it does for me:
Dim d As Double = 1.23456789012346
Dim r As Double = Math.Round(d, 8)
Console.WriteLine("{0} - {1} = {2}", d, r, d - r)
给定:
1.23456789012346 - 1.23456789 = 1.23456800338317E-10
请注意,我使用8位数字使其更明显-7将9向上四舍五入,结果并不十分清楚.仍然有效.
Note I used 8 digits to make it more obvious - 7 rounds the 9 up and it isn''t as clear in the results. Still works though.
首先,Math.Round
始终有效,尤其是如果您正确拼写:-).你为什么要告诉我们一些不正确的东西?
更重要的是,如果您真正使用浮点数,则在几乎所有情况下,将它们四舍五入后再放入数据库是犯罪.您不需要它.数据库存储的浮点值绝不是任何最终"数字,因此以后可以在某些计算中用作中间值或输入值.和中间值的舍入误差趋于累积.
即使您可能需要在屏幕上显示四舍五入的值,这也是在数据库中存储四舍五入的值的非常不好的借口.您永远不需要它.如果您需要四舍五入的表示形式,则实际上不是四舍五入的,而是使用string.Format
进行格式化.请参阅:
http://msdn.microsoft.com/en-us/library/system.string. format.aspx [^ ],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx [ ^ ].
最后两个链接中显示的格式说明符将帮助您提供所需的数据表示形式. 不要将字符串形式放在您的数据库中!
—SA
First,Math.Round
always works, especially if you spell it correctly :-). Why would you tell us something which is not true?
More importantly, if you really work with floating-point numbers, rounding them before putting them in a database is a crime in nearly all situations. You don''t need it. Database-stored floating-point values are never any "final" figures, then can later be used in some calculations as the intermediate or input values; and rounding errors in intermediate values tends to accumulate.
Even though you might need rounded values to be presented on screen, this is a very bad excuse for storing rounded values in the database. You never need it. If you need rounded presentation, this is not actually rounded, but formatting using, say,string.Format
. Please see:
http://msdn.microsoft.com/en-us/library/system.string.format.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].
Format specifiers shown in the last two links will help you to provide required presentation of data. Don''t put it the string form in your database!
—SA
这篇关于如何舍入从数据集中存储的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!