简体   繁体   English

Excel VBA四舍五入与双打

[英]Excel VBA rounding with doubles

I am trying to do a very accurate calculation using doubles that relate to cells in my worksheet. 我正在尝试使用与工作表中的单元格相关的双精度进行非常准确的计算。 As I test I wrote the following code: 在我测试时,我编写了以下代码:

Sub MacroTest()

Dim opt As Worksheet
Set opt = Sheets("Optimisation")

Dim test As Double

test = CDec(opt.Cells(2, 10))

End Sub

In the cell J2 is the decimal value -£3,298.8599993... however when debugging the code it is clear that the test double is only picking up -3298.86 as the value. 在单元格J2中是十进制值 - £3,298.8599993 ...但是在调试代码时,很明显测试双精度值仅为-3298.86。 I don't understand why this would be as I thought doubles were more accurate than this. 我不明白为什么会这样,因为我认为双打比这更准确。 Most of the posts I have read seem to think that using CDec should sort this problem out, however as shown this does not seem to work for me. 我读过的大多数帖子似乎都认为使用CDec应该排除这个问题,但是如图所示,这对我来说似乎不起作用。

Note - The value in J2 has been calculated using a formula, not sure if this makes any difference. 注意 - J2中的值是使用公式计算的,不确定这是否有任何区别。 Any help is greatly appreciated - thanks. 非常感谢任何帮助 - 谢谢。

You should use opt.Cells(2, 10).Value2 in this case. 在这种情况下opt.Cells(2, 10).Value2您应该使用opt.Cells(2, 10).Value2 Range.Value2 property : Range.Value2属性

The only difference between this property and the Value property is that the Value2 property doesn't use the Currency and Date data types. 此属性与Value属性之间的唯一区别是Value2属性不使用Currency和Date数据类型。 You can return values formatted with these data types as floating-point numbers by using the Double data type. 您可以使用Double数据类型将使用这些数据类型格式化的值作为浮点数返回。

So the default .Value will return Currency if the cell is formatted as currency while .Value2 will return a Double nevertheless. 因此,如果单元.Value2为货币,则默认.Value将返回Currency ,而.Value2将返回Double

I can't comment yet, but here is what is wrong with your code: 我还不能发表评论,但是你的代码出了什么问题:

You are using a CDec-conversion formula on a double-datatype, and the result from this function is put into a double-datatype again. 您正在使用双数据类型的CDec转换公式,并且此函数的结果再次置于双数据类型中。 So you do not gain anything here accuracy wise. 所以你在这里没有获得任何准确性。

Are you sure you want to use the double-datatype, while you could use the decimal-datatype in VBA? 您确定要使用double数据类型,而在VBA中可以使用decimal-datatype吗?

In my example, cell "A1" contains the decimal number 3298.859999. 在我的示例中,单元格“A1”包含十进制数3298.859999。

Dim varDec As Variant
varDec = CDec(Range("A1").Value2)

Debug.Print "Value in A1 : " & Format(varDec, "0.0000000000000000000000")

Direct casting of a variable to the Decimal-datatype is not possible in VBA, but the Variant-datatype has the Decimal-datatype as a subtype. 在VBA中无法将变量直接转换为Decimal-datatype,但Variant-datatype具有Decimal-datatype作为子类型。

To see this for yourself: look in the "Locals"-window in the VBE while debugging. 要自己看一下:在调试时查看VBE中的“Locals”窗口。 It will show: 它将显示:

Expression : var
Value      : 3298.859999
Type       : Variant/Decimal

Background article on Excel numeric precision 关于Excel数字精度的背景文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM