# Looking for a Round "to larger value" code!

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

1. ### Anders JanssonGuest

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

val = -99.2
zzz = Abs(Int(-val) ) : zzz = 99

Any suggestions would be great!!
/Thanks

Anders Jansson, Apr 4, 2005

2. ### McKirahanGuest

"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
>
> 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

3. ### Anders JanssonGuest

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
> >
> > 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. ### Anders JanssonGuest

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:

>
>
> 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
5. ### Hal RosserGuest

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
6. ### dlbjrGuest

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

'dlbjr

dlbjr, Apr 5, 2005
7. ### Hal RosserGuest

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

Suppose the arg is already an int ?
Would this code increase it by one ?

Hal Rosser, Apr 6, 2005
8. ### dlbjrGuest

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

dlbjr, Apr 6, 2005
9. ### Hal RosserGuest

"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
10. ### 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
11. ### dlbjrGuest

Not much for one liners. Most like more of a discussion!
I usually wrap logic in a function.
All is good!
--
'dlbjr

dlbjr, Apr 6, 2005
12. ### Dave AndersonGuest

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
13. ### 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
14. ### Mark SchuppGuest

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

Math.ceil()

--
Mark Schupp
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
15. ### 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

It's only two steps upstream.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Evertjan., Apr 7, 2005
16. ### Mark SchuppGuest

"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
>
> 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