Truncating Double with VBA in excel

Ehudz picture Ehudz · Jul 5, 2012 · Viewed 63.2k times · Source

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?

Answer

Siddharth Rout picture Siddharth Rout · Jul 5, 2012

You can either use ROUND for FORMAT in VBA

For example to show 2 decimal places

Dval = 1.56789

Debug.Print Round(dVal,2)

Debug.Print Format(dVal,"0.00")

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