Looking for a Round "to larger value" code!

Discussion in 'ASP General' started by Anders Jansson, Apr 4, 2005.

  1. Hi all!

    I've been searching for a rounding function or some other means to round a
    decimal number "to larger", always, but it can't find it.

    I have been looking at the Fix(), Int() and CInt() functions, but all of
    them behaves in ways making hard to make this happend.

    Some examples:

    - Int(val) Fix(val) : Truncates, except if val is negative, then they
    behave different!
    - CInt(val) : quote "CInt function always rounds it to the nearest even
    number. For example, 0.5 rounds to 0, and 1.5 rounds to 2." end quote.
    So even if I was to add an offset to the val inside the call like
    CInt(val + 0.5),
    this doesn't do it if val is = 0.499,
    CInt(0.499 + 0.5) = 0, since that is the nearest even number!!

    What I want is:
    zzz = xxx(2,3) : Wanted value is zzz = 3
    zzz = xxx(44.9) : Wanted value zzz = 45
    zzz = xxx(33.0001) : Wanted value is zzz = 34
    zzz = xxx(-33.001) : Wanted value is zzz = 34

    Perhaps this could work for possitive values:
    val = 99.2
    zzz = Abs(Int(-val) ) : zzz = 100

    But what about:
    val = -99.2
    zzz = Abs(Int(-val) ) : zzz = 99

    Any suggestions would be great!!
    /Thanks
     
    Anders Jansson, Apr 4, 2005
    #1
    1. Advertising

  2. Anders Jansson

    McKirahan Guest

    "Anders Jansson" <> wrote in message
    news:...
    > Hi all!
    >
    > I've been searching for a rounding function or some other means to round a
    > decimal number "to larger", always, but it can't find it.
    >
    > I have been looking at the Fix(), Int() and CInt() functions, but all of
    > them behaves in ways making hard to make this happend.
    >
    > Some examples:
    >
    > - Int(val) Fix(val) : Truncates, except if val is negative, then they
    > behave different!
    > - CInt(val) : quote "CInt function always rounds it to the nearest even
    > number. For example, 0.5 rounds to 0, and 1.5 rounds to 2." end quote.
    > So even if I was to add an offset to the val inside the call like
    > CInt(val + 0.5),
    > this doesn't do it if val is = 0.499,
    > CInt(0.499 + 0.5) = 0, since that is the nearest even number!!
    >
    > What I want is:
    > zzz = xxx(2,3) : Wanted value is zzz = 3
    > zzz = xxx(44.9) : Wanted value zzz = 45
    > zzz = xxx(33.0001) : Wanted value is zzz = 34
    > zzz = xxx(-33.001) : Wanted value is zzz = 34
    >
    > Perhaps this could work for possitive values:
    > val = 99.2
    > zzz = Abs(Int(-val) ) : zzz = 100
    >
    > But what about:
    > val = -99.2
    > zzz = Abs(Int(-val) ) : zzz = 99
    >
    > Any suggestions would be great!!
    > /Thanks


    Will this help? Watch for word-wrap.

    Dim arrNUM(3)
    arrNUM(0) = 2.3
    arrNUM(1) = 44.9
    arrNUM(2) = 33.0001
    arrNUM(3) = -33.001
    Dim intNUM
    Dim strNUM

    For intNUM = 0 To UBound(arrNUM)
    strNUM = strNUM & arrNUM(intNUM) & " = " & CInt(Abs(arrNUM(intNUM))+.5)
    & vbCrLf
    Next

    WScript.Echo strNUM
     
    McKirahan, Apr 4, 2005
    #2
    1. Advertising

  3. Thanks for the answer!

    I think this will do it!!

    function RoundUp(val)
    Dim retVal
    if Val < 0 then
    retVal = Int(val)
    else
    retVal Abs(Int(-val))
    end if
    RoundUp = retVal
    end function


    "McKirahan" wrote:

    > "Anders Jansson" <> wrote in message
    > news:...
    > > Hi all!
    > >
    > > I've been searching for a rounding function or some other means to round a
    > > decimal number "to larger", always, but it can't find it.
    > >
    > > I have been looking at the Fix(), Int() and CInt() functions, but all of
    > > them behaves in ways making hard to make this happend.
    > >
    > > Some examples:
    > >
    > > - Int(val) Fix(val) : Truncates, except if val is negative, then they
    > > behave different!
    > > - CInt(val) : quote "CInt function always rounds it to the nearest even
    > > number. For example, 0.5 rounds to 0, and 1.5 rounds to 2." end quote.
    > > So even if I was to add an offset to the val inside the call like
    > > CInt(val + 0.5),
    > > this doesn't do it if val is = 0.499,
    > > CInt(0.499 + 0.5) = 0, since that is the nearest even number!!
    > >
    > > What I want is:
    > > zzz = xxx(2,3) : Wanted value is zzz = 3
    > > zzz = xxx(44.9) : Wanted value zzz = 45
    > > zzz = xxx(33.0001) : Wanted value is zzz = 34
    > > zzz = xxx(-33.001) : Wanted value is zzz = 34
    > >
    > > Perhaps this could work for possitive values:
    > > val = 99.2
    > > zzz = Abs(Int(-val) ) : zzz = 100
    > >
    > > But what about:
    > > val = -99.2
    > > zzz = Abs(Int(-val) ) : zzz = 99
    > >
    > > Any suggestions would be great!!
    > > /Thanks

    >
    > Will this help? Watch for word-wrap.
    >
    > Dim arrNUM(3)
    > arrNUM(0) = 2.3
    > arrNUM(1) = 44.9
    > arrNUM(2) = 33.0001
    > arrNUM(3) = -33.001
    > Dim intNUM
    > Dim strNUM
    >
    > For intNUM = 0 To UBound(arrNUM)
    > strNUM = strNUM & arrNUM(intNUM) & " = " & CInt(Abs(arrNUM(intNUM))+.5)
    > & vbCrLf
    > Next
    >
    > WScript.Echo strNUM
    >
    >
    >
     
    Anders Jansson, Apr 4, 2005
    #3
  4. Or even better!

    function RoundUp(val)
    if Val < 0 then
    RoundUp = Int(val)
    else
    RoundUp = Abs(Int(-val))
    end if
    end function


    "Anders Jansson" wrote:

    >
    > Thanks for the answer!
    >
    > I think this will do it!!
    >
    > function RoundUp(val)
    > Dim retVal
    > if Val < 0 then
    > retVal = Int(val)
    > else
    > retVal Abs(Int(-val))
    > end if
    > RoundUp = retVal
    > end function
    >
    >
    > "McKirahan" wrote:
    >
    > > "Anders Jansson" <> wrote in message
    > > news:...
    > > > Hi all!
    > > >
    > > > I've been searching for a rounding function or some other means to round a
    > > > decimal number "to larger", always, but it can't find it.
    > > >
    > > > I have been looking at the Fix(), Int() and CInt() functions, but all of
    > > > them behaves in ways making hard to make this happend.
    > > >
    > > > Some examples:
    > > >
    > > > - Int(val) Fix(val) : Truncates, except if val is negative, then they
    > > > behave different!
    > > > - CInt(val) : quote "CInt function always rounds it to the nearest even
    > > > number. For example, 0.5 rounds to 0, and 1.5 rounds to 2." end quote.
    > > > So even if I was to add an offset to the val inside the call like
    > > > CInt(val + 0.5),
    > > > this doesn't do it if val is = 0.499,
    > > > CInt(0.499 + 0.5) = 0, since that is the nearest even number!!
    > > >
    > > > What I want is:
    > > > zzz = xxx(2,3) : Wanted value is zzz = 3
    > > > zzz = xxx(44.9) : Wanted value zzz = 45
    > > > zzz = xxx(33.0001) : Wanted value is zzz = 34
    > > > zzz = xxx(-33.001) : Wanted value is zzz = 34
    > > >
    > > > Perhaps this could work for possitive values:
    > > > val = 99.2
    > > > zzz = Abs(Int(-val) ) : zzz = 100
    > > >
    > > > But what about:
    > > > val = -99.2
    > > > zzz = Abs(Int(-val) ) : zzz = 99
    > > >
    > > > Any suggestions would be great!!
    > > > /Thanks

    > >
    > > Will this help? Watch for word-wrap.
    > >
    > > Dim arrNUM(3)
    > > arrNUM(0) = 2.3
    > > arrNUM(1) = 44.9
    > > arrNUM(2) = 33.0001
    > > arrNUM(3) = -33.001
    > > Dim intNUM
    > > Dim strNUM
    > >
    > > For intNUM = 0 To UBound(arrNUM)
    > > strNUM = strNUM & arrNUM(intNUM) & " = " & CInt(Abs(arrNUM(intNUM))+.5)
    > > & vbCrLf
    > > Next
    > >
    > > WScript.Echo strNUM
    > >
    > >
    > >
     
    Anders Jansson, Apr 4, 2005
    #4
  5. Anders Jansson

    Hal Rosser Guest

    I think this will work

    If ( (x mod 1) > 0) Then x = x\1 + 1

    'if the remainder of dividing the number by one results in something other
    than zero'
    then add 1 to the result of integer-division x by 1

    if x is 34.44 then x mod 1 is .44 - which is gt zero - which makes the
    condition True
    If x was a whole number, it would not need to be rounded.
    also - is x is 34.44, then x \ 1 is 34 ---- then add 1 makes it 35 - which
    rounds it up
     
    Hal Rosser, Apr 5, 2005
    #5
  6. Anders Jansson

    dlbjr Guest

    Function getCeiling(dbl1)
    getCeiling = CInt(dbl1 + .5)
    End Function


    'dlbjr
    'Pleading sagacious indoctrination!
     
    dlbjr, Apr 5, 2005
    #6
  7. Anders Jansson

    Hal Rosser Guest

    "dlbjr" <> wrote in message
    news:...
    > Function getCeiling(dbl1)
    > getCeiling = CInt(dbl1 + .5)
    > End Function
    >
    >
    > 'dlbjr
    > 'Pleading sagacious indoctrination!
    >


    Suppose the arg is already an int ?
    Would this code increase it by one ?
     
    Hal Rosser, Apr 6, 2005
    #7
  8. Anders Jansson

    dlbjr Guest

    Yes! Here is what you need.

    Function Ceiling(byval n)
    i = CInt(n)
    if i = n then
    Ceiling = n
    Else
    Ceiling = i + 1
    End If
    End Function

    'dlbjr
    'Pleading sagacious indoctrination!
     
    dlbjr, Apr 6, 2005
    #8
  9. Anders Jansson

    Hal Rosser Guest

    "dlbjr" <> wrote in message
    news:...
    > Yes! Here is what you need.
    >
    > Function Ceiling(byval n)
    > i = CInt(n)
    > if i = n then
    > Ceiling = n
    > Else
    > Ceiling = i + 1
    > End If
    > End Function


    ok - your function would now work - but - What's wrong with this one-liner:

    If ( (x mod 1) > 0) Then x = x\1 + 1

    (assuming "x" is the value to be rounded up):
     
    Hal Rosser, Apr 6, 2005
    #9
  10. Anders Jansson

    Evertjan. Guest

    Hal Rosser wrote on 06 apr 2005 in
    microsoft.public.inetserver.asp.general:

    >
    > "dlbjr" <> wrote in message
    > news:...
    >> Yes! Here is what you need.
    >>
    >> Function Ceiling(byval n)
    >> i = CInt(n)
    >> if i = n then
    >> Ceiling = n
    >> Else
    >> Ceiling = i + 1
    >> End If
    >> End Function

    >
    > ok - your function would now work - but - What's wrong with this
    > one-liner:
    >
    > If ( (x mod 1) > 0) Then x = x\1 + 1
    >
    > (assuming "x" is the value to be rounded up):


    ASP vbscript:

    If x > CInt(x) then x = CInt(x) + 1

    ASP jscript:

    x = Math.ceil(x)

    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)
     
    Evertjan., Apr 6, 2005
    #10
  11. Anders Jansson

    dlbjr Guest

    Not much for one liners. Most like more of a discussion!
    I usually wrap logic in a function.
    All is good!
    --
    'dlbjr
    'Pleading sagacious indoctrination!
     
    dlbjr, Apr 6, 2005
    #11
  12. Evertjan. wrote:
    > ASP vbscript:
    >
    > If x > CInt(x) then x = CInt(x) + 1


    It never makes sense to use CInt() when you want Int() or Fix(), since
    CInt(1.5) = CInt(2.5).



    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms. Please do not contact
    me directly or ask me to contact you directly for assistance. If your
    question is worth asking, it's worth posting.
     
    Dave Anderson, Apr 6, 2005
    #12
  13. Anders Jansson

    Evertjan. Guest

    Dave Anderson wrote on 06 apr 2005 in
    microsoft.public.inetserver.asp.general:

    > Evertjan. wrote:
    >> ASP vbscript:
    >>
    >> If x > CInt(x) then x = CInt(x) + 1

    >
    > It never makes sense to use CInt() when you want Int() or Fix(), since
    > CInt(1.5) = CInt(2.5).


    You are Abs(right).

    If x > Int(x) then x = Int(x) + 1

    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)
     
    Evertjan., Apr 7, 2005
    #13
  14. Anders Jansson

    Mark Schupp Guest

    I'm a little surprised no one has mentioned JScript's Math object which has
    a ceiling function.

    Math.ceil()

    --
    Mark Schupp
    Head of Development
    Integrity eLearning
    www.ielearning.com


    "Dave Anderson" <> wrote in message
    news:...
    > Evertjan. wrote:
    > > ASP vbscript:
    > >
    > > If x > CInt(x) then x = CInt(x) + 1

    >
    > It never makes sense to use CInt() when you want Int() or Fix(), since
    > CInt(1.5) = CInt(2.5).
    >
    >
    >
    > --
    > Dave Anderson
    >
    > Unsolicited commercial email will be read at a cost of $500 per message.

    Use
    > of this email address implies consent to these terms. Please do not

    contact
    > me directly or ask me to contact you directly for assistance. If your
    > question is worth asking, it's worth posting.
    >
    >
     
    Mark Schupp, Apr 7, 2005
    #14
  15. Anders Jansson

    Evertjan. Guest

    Mark Schupp wrote on 07 apr 2005 in
    microsoft.public.inetserver.asp.general:

    > I'm a little surprised no one has mentioned JScript's Math object
    > which has a ceiling function.
    >
    > Math.ceil()
    >


    I am a little more surprised
    you didn't read my posting in this thread, Mark.

    It's only two steps upstream.

    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)
     
    Evertjan., Apr 7, 2005
    #15
  16. Anders Jansson

    Mark Schupp Guest

    "Evertjan." <> wrote in message
    news:Xns9631E2006EE3Eeejj99@194.109.133.29...
    > Mark Schupp wrote on 07 apr 2005 in
    > microsoft.public.inetserver.asp.general:
    >
    >> I'm a little surprised no one has mentioned JScript's Math object
    >> which has a ceiling function.
    >>
    >> Math.ceil()
    >>

    >
    > I am a little more surprised
    > you didn't read my posting in this thread, Mark.
    >
    > It's only two steps upstream.
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Replace all crosses with dots in my emailaddress)


    Sorry, Must have missed it. Wasn't paying real close attention to all the
    math stuff

    --
    --Mark Schupp
    Head of Development
    Integrity eLearning
    www.ielearning.com
     
    Mark Schupp, Apr 8, 2005
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Einstein
    Replies:
    4
    Views:
    415
  2. Replies:
    0
    Views:
    1,129
  3. Catherine
    Replies:
    2
    Views:
    342
    John Nagle
    Oct 8, 2007
  4. OliverMarchand

    Float.round - should it be round-to-even

    OliverMarchand, Apr 12, 2006, in forum: Ruby
    Replies:
    2
    Views:
    252
    OliverMarchand
    Apr 12, 2006
  5. cerr

    reading file round and round

    cerr, Mar 19, 2010, in forum: Perl Misc
    Replies:
    6
    Views:
    200
    Peter J. Holzer
    Mar 20, 2010
Loading...

Share This Page