Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding a Number UP to the Nearest Hundredth 2

Status
Not open for further replies.

iamedwardkim

IS-IT--Management
Nov 7, 2002
29
US
Is there an easy way to round numbers up to the nearest hundredth (e.g. 7.83942 and 7.83021 would both be rounded up to 7.84)? Thanks in advance.
 
This is probably to simplistic, but it at least can work for the specific examples:


Code:
Public Function basRndUp2(MyVal As Double, RndVal As Single) As Double

    'Michael Red    11/25/02
    'Sample Usage
    '? basRndUp2(7.83942, 0.01)
    ' 7.84

    '? basRndUp2(7.83021 , 0.01)
    ' 7.84


    If (Round(MyVal, 2) = Round(MyVal + (RndVal / 2), 2)) Then
        basRndUp2 = Round(MyVal, 2)
     Else
        basRndUp2 = Round(MyVal + (RndVal / 2), 2)
    End If

End Function


MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 

select int(yournum*100+1)/100.00


rudy
 
r937,

wouldn't a number like 7.83 be rounded to 7.84 in the solution you provided?
 
edward, yes it would, thanks

let me try that again --

[tt]select int( (yournum+0.00999999)*100 )/100.00[/tt]


rudy
 
hi paul
did you have an idea how to get this rounded value
sample

from 1 to 4.9 the value is equal to 5
then from 5.1 to 9.9 value is equal to 10
just adding 5

thanks in advance
DEK
 
About the best I could do is this
IIf([NumberField]Mod 5 = 0,[NumberField],(5 - (Int([NumberField)Mod 5))+ Int([NumberField])

Paul
 
DEK, I found that in the Debug window it worked without any problems, but when I ran it in a query I had some Double Precision Floating Point Error. For example the value
9.99 Mod 5
returned 0 which it should not return. It should return 4. If you have issues let me know and I'll work on it a little more.

Paul
 
Code:
Public Function basRnd2Val(ValIn As Single, Rnd2 As Single) As Single

    'Michael Red    11/27/02    Tek-Tips thread701-414063

    '? basRnd2Val(7.49, 3.625)
    ' 10.875

    '? basRnd2Val(7.49, 3)
    ' 9

    '? basRnd2Val(7.49, 0.01)
    '7.5

    Dim ModRmdr As Single
    Dim ValPart() As String
    Dim DecPart() As String
    Dim Shift(2) As Integer
    Dim MyVal As Long
    Dim MyRnd As Long
    Dim MyRtn As Long

    ValPart = Split(Str(ValIn), ".")
    DecPart = Split(Str(Rnd2), ".")

    If (UBound(ValPart) > 0) Then
        Shift(0) = Len(ValPart(1))
     Else
        Shift(0) = 0
    End If

    If (UBound(DecPart) > 0) Then
        Shift(1) = Len(DecPart(1))
     Else
        Shift(1) = 0
    End If

    If (Shift(0) > Shift(1)) Then
        Shift(2) = Shift(0)
     Else
        Shift(2) = Shift(1)
    End If

    MyVal = Val(ValIn) * (10 ^ Shift(2))
    MyRnd = Val(Rnd2) * (10 ^ Shift(2))

    MyRtn = MyVal - (MyVal Mod MyRnd) + MyRnd
    basRnd2Val = MyRtn * 10 ^ (-1 * Shift(2))

End Function
[code]
 MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top