Reputation:
I need to display 45.556 as 45.55 what is the format i need to use in true dbgrid pro 7.0.
Upvotes: 1
Views: 54743
Reputation: 31
Actually you can do it in two different ways with different results. We will take the first two digits after the dot symbol "." to get the result: 45.55 out of 45.556
Dim a As Double 'This must be DOUBLE not long
a = 45.556
MsgBox Left(a, InStr(a, ".") + 2) 'Result: 45.55
Programmatically is 100% right, but logically the result above is WRONG 45.556 should be 45.56 because the last digit we want to get rid of is bigger than 5 out of 10 which is 6 so we have to add 1 to the previous number which is 5 to be 6 to have the result: 45.56. It's math not me. The other way will give us the right result with digit length needed:
Dim a As Double 'This must be DOUBLE not long
a = 45.556
MsgBox Format(a, "#.##") 'Result: 45.56
Before "." it's enough to use one #, but after "." we add # as the number of digits we need to display.
Upvotes: 1
Reputation: 33834
Optionally without resulting to string manipulation that will blow up in European countries (which use a , instead of a . for the decimal place)... However, if any combination of floatval and decimalPlaces > 9 you are going to have overflow issues...
Public Function Floor(ByVal floatval As Double, optional decimalPlaces as Long = 0) As Long
Dim intval As Long
intval = Round(floatval)
If intval > floatval Then
intval = intval - 1
End If
if decimalPlaces > 0 then
floatval = float / (10 ^ decimalPlaces)
end if
Floor = intval
End Function
Upvotes: 1
Reputation: 20091
Sorry not familiar with dbgird pro 7.0. If you are looking not looking to trucate the 6 as you showed in your example (rounded 45.556 is 45.56) you can use the format command, which will format your number to two decimal places, rounding accordingly.
format(*value*, "0.00")
Using "0.00" formats the number to default to zero in the position the zero is in.
Using "#.##" formats the number to default to space (nothing)
If you don't want to round the number and are only looking to get the number plus the right two decimal places.
Left(cStr(value), instr(cStr(value), ".") + 2))
Retrieves from the left your number plus 2 past the decimal, truncating the rest. You may not need to cStr(), as VB may explicitly convert it first.
using cStr() may create a space before the number which is from a minus sign would go, format() doesn't do this, if you see this issue in your grid.
Upvotes: 9
Reputation: 59185
To handle this through the grids properties, set the column's NumberFormat = Fixed, unless it is a currency value, then you should use Currency.
Upvotes: 1