Rounder A Number Up!!

A

Adam Knight

I am dividing a value by two..what i need is to round the number up if it is
not a whole number.

Any suggestions on how to do this ??

Help appreciated!!

AK
 
K

Ken Schaefer

See if the number is not an integer, and if so, convert it to an integer and
add one.

Cheers
Ken

: I am dividing a value by two..what i need is to round the number up if it
is
: not a whole number.
:
: Any suggestions on how to do this ??
:
: Help appreciated!!
:
: AK
:
:
 
B

bart plessers

Cint(number+0.5)

or

Cint(number) + 1

--

==========================================
Hyper A.R.T.
bart plessers
Paul Van Ostaijenlaan 4
3001 Heverlee
BELGIUM
tel: +32 (16) 23.40.85
fax: +32 (16) 23.41.06
==========================================
 
A

Andrew J Durstewitz

Actually, I just dealt with this...

intScreens = (cnItems.RecordCount Mod 15) + Int(cnItems.RecordCount /
15)

MOD will return a zero if there is no remainder and a one if there is.
Int forces the number to be a whole number, therefore getting ride of
everything past the decimal.

I'm sure you can figure out how to apply this to what you need to do.

hth,
Andrew

DEVBuilder.org, http://www.DEVBuilder.org
ASP,ASP.NET,VB.NET,PHP,Java,and SQL Support, all in one place.
 
D

Dan Brussee

Actually, I just dealt with this...

intScreens = (cnItems.RecordCount Mod 15) + Int(cnItems.RecordCount /
15)

MOD will return a zero if there is no remainder and a one if there is.
Int forces the number to be a whole number, therefore getting ride of
everything past the decimal.

I'm sure you can figure out how to apply this to what you need to do.

MOD does not return zero or one. The MOD operator returns the
remainder after division. It can be a very helpful operator in certain
cases.

A typical case where I have used it is to cycle through a number of
items, once per day. I take a date, do some date math to determine the
number of days since that date till now, then use MOD to figure out
which item to display...

s(0) = "First Item"
s(1) = "Second Item"
s(2) = "Third Item"
s(3) = "Fourth Item"
s(4) = "Fifth Item"

itm = s(datediff("d", "9/11/2001", now) mod 5)

After dividing the number of days since 9/11, the MOD operator returns
the remainder. Let's say there were only 2 days since 9/11. 2 MOD 5
returns 2, so the s(2) would be returned. On the third day 3 MOD 5
returns 3, so s(3) would be returned.

Later, when it's 303 days after 9/11, 303 MOD 5 returns 3 (303 divided
by 5 is 60 with a remainder of 3).
 
A

Andrew J Durstewitz

My bad, I mis-stated what I was trying to say. Your correct.

However, if you use the MOD the poster will be able to round up.
Basically if the MOD return is greater than 1 it's not a whole number.
Turn it into an integer and add 1. Now it's the next higher number and
whole.

Andrew

DEVBuilder.org, http://www.DEVBuilder.org
ASP,ASP.NET,VB.NET,PHP,Java,and SQL Support, all in one place.
 
D

Dan Brussee

My bad, I mis-stated what I was trying to say. Your correct.

However, if you use the MOD the poster will be able to round up.
Basically if the MOD return is greater than 1 it's not a whole number.
Turn it into an integer and add 1. Now it's the next higher number and
whole.

Dont mean to harp on it, but how would MOD tell you if it's a whole
number? It's really meant to be used with integers

17 MOD 5 returns 2

All numbers are whole.
 
D

Dave Anderson

Adam Knight said:
I am dividing a value by two..what i need is to round the
number up if it is not a whole number.

Whatever you do, avoid CInt. Here's why (see the section on Bankers
Rounding)...
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q196652
....as well as the [Note] here:
http://msdn.microsoft.com/library/en-us/script56/html/vsfctCInt.asp

In short, here's a decent VBScript implementation of a "ceiling" function:

Function RoundUp(val)
RoundUp = Int(val) + Sgn(val - Int(val))
End Function

Just in case you care about negative numbers, consider what behavior you
want for negative values before implementing this. "Rounding up" means
that -1.2 rounds to -1. If you prefer that -1.2 round to -2, use something
like this:

Function RoundUp(val)
RoundUp = Fix(val) + Sgn(val - Fix(val))
End Function

JScript is simpler, in a way. The first example above is merely:

Math.ceil(val)

The second is a bit trickier. Here are two implementations:

1. val/Math.abs(val) * Math.ceil(Math.abs(val))
2. val > 0 ? Math.ceil(val) : Math.floor(val)

Enjoy exploring this.


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

Dave Anderson

Dan Brussee said:
Dont mean to harp on it, but how would MOD tell you if it's
a whole number? It's really meant to be used with integers

"The modulus, or remainder, operator divides number1 by number2 (rounding
floating-point numbers to integers) and returns only the remainder as
result."
http://msdn.microsoft.com/library/en-us/script56/html/vsoprmod.asp

*** HOWEVER ***
Mod uses Banker's Rounding, so it should be avoided at all costs:

1.5 Mod 10 is 2
2.5 Mod 10 is 2


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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top