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?
相关问题
- Do the Java Integer and Double objects have unnece
- Excel sunburst chart: Some labels missing
- Error handling only works once
- Error handling only works once
- Excel formula in VBA code
相关文章
- 关于C#中 float、double、decimal 的运算不精确的问题。
- Get column data by Column name and sheet name
- programmatically excel cells to be auto fit width
- How can I convert a OLE Automation Date value to a
- Unregister a XLL in Excel (VBA)
- Unregister a XLL in Excel (VBA)
- Macro or function to construct a float (double) fr
- How to prevent excel from truncating numbers in a
You can use Int() function.
Debug.print Int(1.99543)
Or Better:
So you can use
Trunc(1.99543, 4)
==>result: 1.9954
So fun story. I was messing around with a quick VB conversion function. I just want to truncate a double into an integer.
Awesome, something in VB actually worked.
Oops, I forgot it doesn't work with negative numbers
... yeah that just happened. VB uses Banker rounding.
If you want specific decimal places do what Makah did only with Abs around the value so Int can truncate properly.
You can either use
ROUND
forFORMAT
in VBAFor example to show 2 decimal places
Note: The above will give you
1.57
. So if you are looking for1.56
then you can store the Dval in a string and then do thisIf 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:
This yields "2.34".