From: hermann leinen on
Hello,

I want to calculate how many rows I need in a grid for a given number of
cells when the number of columns is limited to let's say 6.
Somehow I don't find any better way than using Mod because

lCellCount=1917
lMaxCols = 6

would return 319,5
but VB6 rounds that down to 319 instead of rounding it up.
This means I get 1 row less than I really need.

I was only able to help me by using Mod but that looks fishy/ unncessary
to me.

Here is my code:

Dim lRows&
lRows = lCellCount \ lMaxCols

Dim iMod%
iMod = lCellCount Mod lMaxCols

If iMod > 0 Then
lRows = lRows + 1
End If

Thanks,
Hermann
From: Helmut Meukel on
Hermann,

how about using integer devision this way:
lRows = (lCellCount + lMaxCols -1) \ lMaxCols
So you can ignore the remainder.

Helmut.


"hermann leinen" <h.leinen(a)gmx.de> schrieb im Newsbeitrag
news:%23w57NzHtKHA.732(a)TK2MSFTNGP06.phx.gbl...
> Hello,
>
> I want to calculate how many rows I need in a grid for a given number of cells
> when the number of columns is limited to let's say 6.
> Somehow I don't find any better way than using Mod because
>
> lCellCount=1917
> lMaxCols = 6
>
> would return 319,5
> but VB6 rounds that down to 319 instead of rounding it up.
> This means I get 1 row less than I really need.
>
> I was only able to help me by using Mod but that looks fishy/ unncessary to
> me.
>
> Here is my code:
>
> Dim lRows&
> lRows = lCellCount \ lMaxCols
>
> Dim iMod%
> iMod = lCellCount Mod lMaxCols
>
> If iMod > 0 Then
> lRows = lRows + 1
> End If
>
> Thanks,
> Hermann

From: Dee Earley on
On 23/02/2010 11:37, hermann leinen wrote:
> Hello,
>
> I want to calculate how many rows I need in a grid for a given number of
> cells when the number of columns is limited to let's say 6.
> Somehow I don't find any better way than using Mod because
>
> lCellCount=1917
> lMaxCols = 6
>
> would return 319,5
> but VB6 rounds that down to 319 instead of rounding it up.
> This means I get 1 row less than I really need.

It CInt() will round to the nearest even number, Int() will truncate.

> I was only able to help me by using Mod but that looks fishy/ unncessary
> to me.

This will always round up to the next integer.
If Rows > Int(Rows) then Rows = Int(Rows) + 1

--
Dee Earley (dee.earley(a)icode.co.uk)
i-Catcher Development Team

iCode Systems
From: Larry Serflaten on

"hermann leinen" <h.leinen(a)gmx.de> wrote
> I want to calculate how many rows I need in a grid for a given number of
> cells when the number of columns is limited to let's say 6.
> Somehow I don't find any better way than using Mod because
<...>
> I was only able to help me by using Mod but that looks fishy/ unncessary
> to me.
>
> Here is my code:
<snipped>

As Helmut shows, you can push the number of cells up so that any
remainder puts the result up into the next multiple. That certainly
seems like a good method to me.

Another take on that problem would point out that with exact multiples,
the remainder of the Mod operation would be 0, with the integer division
yielding the correct answer. But, any time there is a remainder, the integer
division is off by one.

So what is needed is a function that will return 0 for 0 and 1 for any of the
other possible values:

rows = cells \ cols + Sgn(cells Mod cols)

Given that the number of cells needed is always a positive number, the
result from the Mod operation will also be a positive number, (if not 0)
so that the result of the Sgn function will be either 0 when you need it
to be 0 or it will be 1 if there is any remainder.

LFS





From: Nobody on
"hermann leinen" <h.leinen(a)gmx.de> wrote in message
news:%23w57NzHtKHA.732(a)TK2MSFTNGP06.phx.gbl...
> I was only able to help me by using Mod but that looks fishy/ unncessary
> to me.
>
> Here is my code:
>
> Dim lRows&
> lRows = lCellCount \ lMaxCols
>
> Dim iMod%
> iMod = lCellCount Mod lMaxCols
>
> If iMod > 0 Then
> lRows = lRows + 1
> End If

Your solution is what many would do, except they won't use a variable for
Mod result. Example:

Dim lRows&
lRows = lCellCount \ lMaxCols
If lCellCount Mod lMaxCols <> 0 Then
lRows = lRows + 1
End If