Looking for a Round "to larger value" code!

A

Anders Jansson

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
 
M

McKirahan

Anders Jansson said:
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
 
A

Anders Jansson

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
 
A

Anders Jansson

Or even better!

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

Hal Rosser

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
 
D

dlbjr

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


'dlbjr
'Pleading sagacious indoctrination!
 
H

Hal Rosser

dlbjr said:
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 ?
 
D

dlbjr

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!
 
H

Hal Rosser

dlbjr said:
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):
 
E

Evertjan.

Hal Rosser wrote on 06 apr 2005 in
microsoft.public.inetserver.asp.general:
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)
 
D

dlbjr

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

Dave Anderson

Evertjan. said:
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.
 
E

Evertjan.

Dave Anderson wrote on 06 apr 2005 in
microsoft.public.inetserver.asp.general:
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
 
M

Mark Schupp

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

Math.ceil()
 
E

Evertjan.

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.
 
M

Mark Schupp

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


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

It's only two steps upstream.

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top