Truncating Double with VBA in excel

前端 未结 4 1232
刺人心
刺人心 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:36

    You can use Int() function. Debug.print Int(1.99543)

    Or Better:

    Public Function Trunc(ByVal value As Double, ByVal num As Integer) As Double
      Trunc = Int(value * (10 ^ num)) / (10 ^ num)
    End Function
    

    So you can use Trunc(1.99543, 4) ==> result: 1.9954

提交回复
热议问题