|
|
Question : Rounding Decimals
|
|
Does any one know how to round decimals. I know how to set the display property to display the number desired. But what functtion do you use to actually change the vaule of the number to the rounded off value So that 1.5 + .049 will actually be not just dispalyed as 1.6 but really change the value to 1.6
|
Answer : Rounding Decimals
|
|
Here is a rounding function for you:
Function wizRound(pAnyNumber, pNoOfDigits) As Double ' Programmed By: THa ' Intention: Round a number to a specified number of digits ' Parameters: pAnyNumber - Number to round ' pNoOfDigits - Wanted number of digits ' Return: Rounded number ' Dummy Return: NULL ' Updates: 12-jul-99 THa: Changed code to match description of potential errors in article ' http://www.fmsinc.com/tpapers/math/index.html On Error Resume Next
Dim dblFactor As Double Dim dblTemp As Double If Nz(pNoOfDigits, 0) >= 0 And Nz(pNoOfDigits, 0) < 20 Then 'OK dblFactor = 10 ^ pNoOfDigits Else dblFactor = 10 ^ 2 End If dblTemp = pAnyNumber * dblFactor + 0.5 wizRound = Int(CDec(dblTemp)) / dblFactor ' Old line of code. wizRound(100.05,1) would give 100 instead of 100.1 'wizRound = Int(pAnyNumber * (1 & String(pNoOfDigits, "0"))) / (1 & String(pNoOfDigits, "0")) End Function
See the link (http://www.fmsinc.com/tpapers/math/index.html) for details on the rounding problem in Access.
BUT, the mathematics are failing you a bit here resulting in the fact the wizRound(1.5 + .049) = 1.5
1.549 rounded to 1 decimal is 1.5, 1.55 rounded will be 1.6 1.549 round to 0 decimals will on the other hand be 2.
|
|
|
|
|