英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论