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

Re: Looking for a Round "to larger value" code! by McKirahan

McKirahan
Mon Apr 04 05:32:57 CDT 2005

"Anders Jansson" <AndersJansson@discussions.microsoft.com> wrote in message
news:1F10D69E-B5A7-4F2F-86AB-A0E78F431DB1@microsoft.com...
> 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



Re: Looking for a Round "to larger value" code! by AndersJansson

AndersJansson
Mon Apr 04 05:43:03 CDT 2005


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" <AndersJansson@discussions.microsoft.com> wrote in message
> news:1F10D69E-B5A7-4F2F-86AB-A0E78F431DB1@microsoft.com...
> > 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
>
>
>

Re: Looking for a Round "to larger value" code! by AndersJansson

AndersJansson
Mon Apr 04 05:47:02 CDT 2005


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" <AndersJansson@discussions.microsoft.com> wrote in message
> > news:1F10D69E-B5A7-4F2F-86AB-A0E78F431DB1@microsoft.com...
> > > 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
> >
> >
> >

Re: Looking for a Round "to larger value" code! by Hal

Hal
Mon Apr 04 18:42:00 CDT 2005

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



Re: Looking for a Round "to larger value" code! by dlbjr

dlbjr
Mon Apr 04 21:12:13 CDT 2005

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


'dlbjr
'Pleading sagacious indoctrination!



Re: Looking for a Round "to larger value" code! by Hal

Hal
Tue Apr 05 20:55:17 CDT 2005


"dlbjr" <oops@iforgot.com> wrote in message
news:uvQsiTYOFHA.3400@TK2MSFTNGP10.phx.gbl...
> 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 ?



Re: Looking for a Round "to larger value" code! by dlbjr

dlbjr
Tue Apr 05 22:09:50 CDT 2005

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!



Re: Looking for a Round "to larger value" code! by Hal

Hal
Tue Apr 05 22:33:50 CDT 2005


"dlbjr" <oops@iforgot.com> wrote in message
news:OGaCZYlOFHA.2680@TK2MSFTNGP09.phx.gbl...
> 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):



Re: Looking for a Round "to larger value" code! by Evertjan

Evertjan
Wed Apr 06 10:31:47 CDT 2005

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

>
> "dlbjr" <oops@iforgot.com> wrote in message
> news:OGaCZYlOFHA.2680@TK2MSFTNGP09.phx.gbl...
>> 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)


Re: Looking for a Round "to larger value" code! by Dave

Dave
Wed Apr 06 16:55:00 CDT 2005

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.



Re: Looking for a Round "to larger value" code! by dlbjr

dlbjr
Wed Apr 06 16:27:08 CDT 2005

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!



Re: Looking for a Round "to larger value" code! by Evertjan

Evertjan
Thu Apr 07 03:14:29 CDT 2005

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)


Re: Looking for a Round "to larger value" code! by Mark

Mark
Thu Apr 07 10:45:08 CDT 2005

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" <GTSPXOESSGOQ@spammotel.com> wrote in message
news:OmNDKNvOFHA.1932@tk2msftngp13.phx.gbl...
> 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.
>
>



Re: Looking for a Round "to larger value" code! by Evertjan

Evertjan
Thu Apr 07 15:12:58 CDT 2005

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)


Re: Looking for a Round "to larger value" code! by Mark

Mark
Fri Apr 08 09:40:35 CDT 2005

"Evertjan." <exjxw.hannivoort@interxnl.net> 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