15

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?

7 Answers 7

15

If you want to round the value, then you can use the Round function (but be aware that VBA's Round function uses Banker's rounding, also known as round-to-even, where it will round a 5 up or down; to round using traditional rounding, use Format).

If you want to truncate the value without rounding, then there's no need to use strings as in the accepted answer - just use math:

Dim lDecimalPlaces As Long: lDecimalPlaces = 2
Dim dblValue As Double: dblValue = 2.345

Dim lScale = 10 ^ lDecimalPlaces
Dim dblTruncated As Double: dblTruncated = Fix(dblValue * lScale) / lScale

This yields "2.34".

1
  • 1
    I had forgotten about the Banker's rounding. +1 despite the condescending "just use math" jab. For just displaying the number as the OP requested, if you don't care much about rounding, Format() is the tool for the job, and is simpler than Fix() (or Int(), which is equivalent to Fix() for positive numbers). However, there are cases where Banker's rounding will cause user questions and require a more complex transformation. Feb 10, 2017 at 0:10
13

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
1
  • 2
    Never handle numbers as text.
    – Gustav
    Oct 7, 2020 at 6:02
9

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

0

This was my attempt:

Function TruncateNumber(decimalNum As Double, decPlaces As Integer) As Double
'decimalNum: the input number to be truncated
'decPlaces: how many decimal places to round to. Use 0 for no decimal places.
    decimalLocation = InStr(decimalNum, ".")
    TruncateNumber = Left(decimalNum, decimalLocation + decPlaces)
End Function

It uses strings to avoid any math errors caused by different rounding methods. It will output as a type double, so you can still perform your own math on it.

This will cause an error if a number without a decimal place is passed into the above function. If this is a concern, you can use the following code instead:

Function TruncateNumber(decimalNum As Double, decPlaces As Integer) As Double
'decimalNum: the input number to be truncated
'decPlaces: how many decimal places to round to. Use 0 for no decimal places.
    If InStr(decimalNum, ".") = 0 Then 'if there was no decimal:
        'then return the number that was given
        TruncateNumber = decimalNum
    Else 'if there is a decimal:
        'then return the truncated value as a type double
        decimalLocation = InStr(decimalNum, ".")
        TruncateNumber = Left(decimalNum, decimalLocation + decPlaces)
    End If
End Function

Hopefully these functions are of some use to someone. I haven't done extensive testing, but they worked for me.

0

EDITED

Newer version of Excel (VBA) have a TRUNC function which already does things properly.

For older versions of EXCEL

I wanted to truncate a double into an integer.

value = Int(83.768)
value == 83

Awesome, it worked.

Depending on your version of Excel (VB) this might not work with negative numbers.

value = Int(-83.768)
value == -84

VB uses Banker rounding.

Public Function Trunc1(ByVal value As Double) As Integer
  ' Truncate by calling Int on the Absolute value then multiply by the sign of the value.
  ' Int cannot truncate doubles that are negative
  Trunc1 = Sgn(value) * Int(Abs(value))
End Function

If you want specific decimal places do what Makah did only with Abs around the value so Int can truncate properly.

Public Function Trunc2(ByVal value As Double, Optional ByVal num As Integer = 1) As Double
    ' Truncate by calling Int on the Absolute value then multiply by the sign of the value.
    ' Int cannot truncate doubles that are negative
    Trunc2 = Sgn(value) * (Int(Abs(value) * (10 ^ num)) / (10 ^ num))
End Function
1
  • There is a sign function that achieves sign = Abs(value) / value in one step: Sgn(value).
    – vpprof
    Aug 9, 2020 at 0:57
0

Here is a little experiment I did... (1st time posting and answer, please tell me if I am not following conventions.

Sub Truncate()
    
Dim dblNum  As Double
Dim intDecimal  As Integer

dblNum = 1578.56789
intDecimal = 2  '0 returns 1578
                '2 returns 1578.56
                '-2 returns 1500

Debug.Print (Int(dblNum * 10 ^ intDecimal) / 10 ^ intDecimal)

End Sub
0

Old question but @Makah's answer did not work, i finally used this:

 Dim trunc as Double
 Dim i as Double
 i = 186.075415614632
 ' 2 is the number of significant digits
 trunc = CDbl(Left(CStr(i), InStr(1, CStr(i), ".") + 2))

Worked fine for me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.