VBA的round(var,1)导致浮点错误。

huangapple go评论66阅读模式
英文:

VBA round(var,1) results in floating point error

问题

I am using VBA to calculate and print a value to a worksheet. The calculated value is a "single" and I want to round it to one decimal place. For some reason, the "round" function rounds to the nearest decimal, but it still has what looks like floating point error on top of it. I can fix this manually, but I'd rather not have to every time and would like to understand what's actually going on.

I used the following code.

mysheet.Cells(row, ctotal).Value = Round(ttime, 1)

In one example, ttime is 2.183333 and the value that gets printed in the corresponding cell is 2.20000004768371. I would like it to print "2.2" in this case, without the rest of the nonsense thereafter. Any ideas?

英文:

I am using VBA to calculate and print a value to a worksheet. The calculated value is a "single" and I want to round it to one decimal place. For some reason, the "round" function rounds to the nearest decimal, but it still has what looks like floating point error on top of it. I can fix this manually, but I'd rather not have to every time and would like to understand what's actually going on.

I used the following code.

mysheet.Cells(row, ctotal).Value = Round(ttime, 1)

In one example, ttime is 2.183333 and the value that gets printed in the corresponding cell is 2.20000004768371. I would like it to print "2.2" in this case, without the rest of the nonsense thereafter. Any ideas?

答案1

得分: 2

I'd use WorksheetFunction.Round, since Round uses bankers rounding.

As a side effect, the return of WorksheetFunction.Round is a Double:

Dim ttime As Single
ttime = 2.183333

Debug.Print TypeName(Round(ttime, 1)) ' returns Single
Debug.Print TypeName(WorksheetFunction.Round(ttime, 1)) ' returns Double

If you do want bankers rounding, then:

Round(CDbl(ttime), 1)

You can control the decimals displayed by changing the cell's .NumberFormat:

mysheet.Cells(row, ctotal).NumberFormat = "0.0"
英文:

I'd use WorksheetFunction.Round, since Round uses bankers rounding.

As a side effect, the return of WorksheetFunction.Round is a Double:

Dim ttime As Single
ttime = 2.183333
    
Debug.Print TypeName(Round(ttime, 1)) ' returns Single
Debug.Print TypeName(WorksheetFunction.Round(ttime, 1)) ' returns Double

If you do want bankers rounding, then:

Round(CDbl(ttime), 1)

You can control the decimals displayed by changing the cell's .NumberFormat:

mysheet.Cells(row, ctotal).NumberFormat = "0.0"

huangapple
  • 本文由 发表于 2023年5月11日 04:24:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定