Truncating Double with VBA in excel

前端 未结 4 1215
刺人心
刺人心 2021-01-04 05:21

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?

4条回答
  •  执笔经年
    2021-01-04 05:50

    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
    

提交回复
热议问题