# Rounder A Number Up!!

Discussion in 'ASP General' started by Adam Knight, Sep 14, 2003.

1. ### Adam KnightGuest

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

Adam Knight, Sep 14, 2003

2. ### Ken SchaeferGuest

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

Cheers
Ken

"Adam Knight" <> wrote in message
news:%...
: 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
:
:

Ken Schaefer, Sep 14, 2003

3. ### bart plessersGuest

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

"Adam Knight" <> wrote in message
news:#...
> 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
>
>

bart plessers, Sep 14, 2003
4. ### ljbGuest

sounds like a ceiling function

http://www.vb2themax.com/Item.asp?PageID=CodeBank&Cat=110&ID=24

vb2themax has the code

Function Ceiling(Number)
Ceiling = -Int(-Number)
End Function

"Adam Knight" <> wrote in message
news:#...
> 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
>
>

ljb, Sep 15, 2003
5. ### Andrew J DurstewitzGuest

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.

Andrew J Durstewitz, Sep 15, 2003
6. ### Dan BrusseeGuest

On 15 Sep 2003 15:56:14 GMT, Andrew J Durstewitz
<> wrote:

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

Dan Brussee, Sep 15, 2003
7. ### Andrew J DurstewitzGuest

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.

Andrew J Durstewitz, Sep 15, 2003
8. ### Dan BrusseeGuest

On 15 Sep 2003 17:26:15 GMT, Andrew J Durstewitz
<> wrote:

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

Dan Brussee, Sep 15, 2003
9. ### Dave AndersonGuest

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

Dave Anderson, Sep 15, 2003
10. ### Dave AndersonGuest

"Dan Brussee" wrote:
>
> 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.

Dave Anderson, Sep 15, 2003