Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncating Double with VBA in excel

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?

like image 612
Ehudz Avatar asked Jul 05 '12 15:07

Ehudz


People also ask

How do you round a double in VBA?

Example #1 – VBA Round Function to Round a Number Step 1: Insert a new module under Visual Basic Editor (VBE). Step 2: Define a new sub-procedure to store a macro in VBE. Step 4: Now, add “& Round (10.9834, 2)” in front of MsgBox command, so that the rounded value will be shown up in the message box along.

How many digits can a double hold VBA?

VBA Double can hold up to 14 digit values if it is under the above-given limit. VBA Double uses 8 Byte of system memory each type it is used.

How do you truncate a formula in Excel?

In the first cell of the column, you want to populate with the truncated number, type an "=" sign to indicate you're using a formula. Then type TRUNC. Use an open parenthesis to add the operators of the formula. The first number you enter is the number you want to truncate.

Is there a way to truncate a double into an integer?

Newer version of Excel (VBA) have a TRUNC function which already does things properly. I wanted to truncate a double into an integer. Awesome, it worked. Depending on your version of Excel (VB) this might not work with negative numbers. VB uses Banker rounding.

How to convert string to double in Excel VBA?

The VBA CDbl function converts an expression to a double. If you insert the function in the VBA, you’ll see the following scenario. Let’s be introduced with today’s dataset where the GDP growth of some countries is given in string data type. And we need to convert the string to double. 1. Convert String to Double and Display Using the MsgBox 1.1.

How does VBA double work?

This is how VBA Double works. VBA Double converts the same number into decimal numbers in a specific range. It is quite easy to use. We can use Double data type instead of Integer or Single, in a similar way. It doesn’t consider the decimal number beyond the limit which we have seen in the above introduction section.

What is a double data type in Excel VBA?

Excel VBA Double Data Type. VBA Double is a kind of data type we assign to declare variables, which is an improved or longer version of the “Single” data type variable and is usually used to store longer decimal places. VBA Integer data type always converts decimal values to the nearest integer value. The single data type can show up ...


2 Answers

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".

like image 174
Gary McGill Avatar answered Oct 01 '22 03:10

Gary McGill


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
like image 29
Siddharth Rout Avatar answered Oct 01 '22 05:10

Siddharth Rout