Velocity Reviews > Looking for a Round "to larger value" code!

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

Anders Jansson
Guest
Posts: n/a

 04-04-2005
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

McKirahan
Guest
Posts: n/a

 04-04-2005
"Anders Jansson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
Guest
Posts: n/a

 04-04-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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
Guest
Posts: n/a

 04-04-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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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
> >
> >
> >

Hal Rosser
Guest
Posts: n/a

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

dlbjr
Guest
Posts: n/a

 04-05-2005
Function getCeiling(dbl1)
getCeiling = CInt(dbl1 + .5)
End Function

'dlbjr
'Pleading sagacious indoctrination!

Hal Rosser
Guest
Posts: n/a

 04-06-2005

"dlbjr" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 ?

dlbjr
Guest
Posts: n/a

 04-06-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!

Hal Rosser
Guest
Posts: n/a

 04-06-2005

"dlbjr" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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):

Evertjan.
Guest
Posts: n/a

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

>
> "dlbjr" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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)

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post DaGenester MCITP 2 05-20-2010 04:36 PM postings@alexshirley.com ASP .Net 0 06-22-2006 11:26 AM Peter Williams HTML 1 06-03-2005 08:17 AM lbbss Computer Support 6 04-14-2005 05:46 PM Alan Computer Support 6 10-08-2004 08:24 AM

Advertisments