在Excel中截断VBA [英] Truncating Double with VBA in excel
本文介绍了在Excel中截断VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要截断我的double值的小数位数,以便在文本框中显示。如何使用vba来实现?
I need to truncate the amount of decimal places of my double value for display in a textbox. How would one achieve this with vba?
推荐答案
您可以使用 ROUND
for FORMAT
in VBA
You can either use ROUND
for FORMAT
in VBA
例如显示2位小数位数
Dval = 1.56789
Debug.Print Round(dVal,2)
Debug.Print Format(dVal,"0.00")
注意:以上将给您 1.57
。所以如果你正在寻找 1.56
,那么你可以将Dval存储在一个字符串中,然后这样做
Note: The above will give you 1.57
. So if you are looking for 1.56
then you can store the Dval in a string and then do this
Dim strVal As String
dVal = 1.56789
strVal = dVal
If InStr(1, strVal, ".") Then
Debug.Print Split(strVal, ".")(0) & "." & Left(Split(strVal, ".")(1), 2)
Else
Debug.Print dVal
End If
这篇关于在Excel中截断VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文