Rounding with CInt 
Author Message
 Rounding with CInt

Quote:

> Why does CInt(0.5) return 0, CINt(1.5) return 2 and CInt(2.5) return 2?
> Help file says it rounds to nearest.  Is this the 'new math' I've been
> hearing about?

I believe the rule is "rounding to the nearest =even= number".
--

    National Library of Canada
    Anti spam note: Do not Reply without correcting the address
        **  None of the above has any official status  **


Mon, 06 Mar 2000 03:00:00 GMT  
 Rounding with CInt

Actually, the rule is round to the nearest integer if there is one.  If
there are two integers equally near, pick the even one.  This logic is
commonly used to reduce the effect of cumulative rounding errors and is
common in the sciences.  If you want a more consistent rule, use
Int(x+0.5).  If you are using currency types, make sure all values and
variables in your expression are currency type to prevent
least-significant-bit problems converting between binary floating point and
decimal fixed point.

Rounding can actually be a pretty complicated subject at times.  For
accounting purposes, it is often necessary to do rounding which is +/-
symmetric which can be done by rounding the absolute value of a number,
then restoring the sign.

Round .5s to higher magnitude: Int( Abs(x)+CCur(.5) ) * Sgn(x)
Round .5s to lower magnitude: Int( -Abs(x)+CCur(.5) ) * -Sgn(x)

--

Developer: Inter-Tec Software Services
 ( Remove ".NOSPAM" from address to reply via e-mail )



Quote:

> > Why does CInt(0.5) return 0, CINt(1.5) return 2 and CInt(2.5) return 2?
> > Help file says it rounds to nearest.  Is this the 'new math' I've been
> > hearing about?

> I believe the rule is "rounding to the nearest =even= number".
> --

>     National Library of Canada
>     Anti spam note: Do not Reply without correcting the address
>         **  None of the above has any official status  **



Mon, 06 Mar 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. CINT function -- vb rounding question

2. Round buttons round forms

3. CINT & INT rounding confusion.

4. VB5: CInt(4.5) = 4; CInt(4.5000000001) = 5?

5. String to Integer conversion using CInt() ?

6. odd cint() behaviour

7. CInt, CType, or DirectCast?

8. VBscript: CInt limitations

9. Getting Cint (0.9) to return 0 instead of 1

10. When to use CInt() or CStr()

11. Cint returns type error message

12. Help: CINT bug in VB3 under Win95?

 

 
Powered by phpBB® Forum Software