-
A couple of generic rounding functions I use:
[VBA]
Function RoundDec(ByVal x As Double, Optional Direction As Integer = 0, Optional DecimalPlaces As Long = 0) As Double
x = x * 10 ^ DecimalPlaces
If Direction < 0 Then
RoundDec = Int(x)
ElseIf Direction > 0 Then
If x = Int(x) Then
RoundDec = Int(x)
Else
RoundDec = Int(x) + 1
End If
Else
If x - Int(x) < 0.5 Then
RoundDec = Int(x)
Else
RoundDec = Int(x) + 1
End If
End If
RoundDec = RoundDec / 10 ^ DecimalPlaces
End Function
Function RoundSF(ExactValue As Double, SigFigs As Integer, Optional Direction As Integer = 0) As Double
Dim x As Integer
x = Int(VBA.Math.Log(ExactValue) / VBA.Math.Log(10)) + 1
RoundSF = Int(RoundDec(ExactValue / 10 ^ (x - SigFigs), Direction)) * 10 ^ (x - SigFigs)
End Function
[/VBA]
For rounding up, down, nearest to decimal places or significant figures.
"Computers are useless. They can only give you answers." - Pablo Picasso
Mark Rowlinson FIA |
The Code Net
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules