473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Rounding when converting float to integer

Br
Cint(3/2) = 2 (rounded up, I would have thought the answer would be 1)

Cint(5/2) = 2 (rounded down)

The Access help says Cint() rounds to the nearest even number, but why??

Do I need to "roll my own" integer conversion to get it to always round up
or down?

I looked at Cint(3\2) and Cint(5\2) which seems to consistently round
down... that may do.
--
regards,

Br@dley
Apr 13 '06 #1
15 15016
Br@dley wrote:
Cint(3/2) = 2 (rounded up, I would have thought the answer would be 1)

Cint(5/2) = 2 (rounded down)

The Access help says Cint() rounds to the nearest even number, but why??

Do I need to "roll my own" integer conversion to get it to always round up
or down?

I looked at Cint(3\2) and Cint(5\2) which seems to consistently round
down... that may do.


Format(3/2) -> 1.5 a string. To get a number use Val(Format(3/2)).

Int(Format(3/2)) -> 1. Int() removes the decimal.

Format(5/2) -> 2.5 a string.

Val(Format(5/2)) -> 2.5. Int(Format(5/2)) - > 2.

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
Apr 13 '06 #2
Public Function Ceiling(ByVal d As Double) As Long
Ceiling = -(Int(-d))
End Function

Apr 13 '06 #3
Br
MGFoster wrote:
Br@dley wrote:
Cint(3/2) = 2 (rounded up, I would have thought the answer would be
1) Cint(5/2) = 2 (rounded down)

The Access help says Cint() rounds to the nearest even number, but
why?? Do I need to "roll my own" integer conversion to get it to always
round up or down?

I looked at Cint(3\2) and Cint(5\2) which seems to consistently round
down... that may do.


Format(3/2) -> 1.5 a string. To get a number use Val(Format(3/2)).

Int(Format(3/2)) -> 1. Int() removes the decimal.

Format(5/2) -> 2.5 a string.

Val(Format(5/2)) -> 2.5. Int(Format(5/2)) - > 2.


Ta. I guess my main question was WHY the Cint() rounds to the nearest even
number?? Just interested in the reasoning for implementing it that way.
--
regards,

Br@dley
Apr 13 '06 #4
"Br@dley" <do***********@google.com> wrote in
news:e1**********@news-02.connect.com.au:
MGFoster wrote:
Br@dley wrote:
Cint(3/2) = 2 (rounded up, I would have thought the answer would be
1) Cint(5/2) = 2 (rounded down)

The Access help says Cint() rounds to the nearest even number, but
why?? Do I need to "roll my own" integer conversion to get it to
always round up or down?

I looked at Cint(3\2) and Cint(5\2) which seems to consistently
round down... that may do.


Format(3/2) -> 1.5 a string. To get a number use Val(Format(3/2)).

Int(Format(3/2)) -> 1. Int() removes the decimal.

Format(5/2) -> 2.5 a string.

Val(Format(5/2)) -> 2.5. Int(Format(5/2)) - > 2.


Ta. I guess my main question was WHY the Cint() rounds to the nearest
even number?? Just interested in the reasoning for implementing it
that way.


So that the sum of a bunch of rounded numbers will approximate the sum of
the same numbers before rounding.

--
Lyle Fairfield
Apr 13 '06 #5
Br@dley wrote in message <e1**********@news-02.connect.com.au> :
Cint(3/2) = 2 (rounded up, I would have thought the answer would be
1)

Cint(5/2) = 2 (rounded down)

The Access help says Cint() rounds to the nearest even number, but
why??

Do I need to "roll my own" integer conversion to get it to always
round up or down?

I looked at Cint(3\2) and Cint(5\2) which seems to consistently round
down... that may do.


Check out these (watch for linebreaks in the links)
http://msdn.microsoft.com/library/de...truncation.asp
http://support.microsoft.com/default...b;EN-US;196652

--
Roy-Vidar
Apr 13 '06 #6
Lyle Fairfield schreef:
Ceiling = -(Int(-d))


Wow! A quote from the Excel 3.0 manual ... ;-)

--
Paul
Apr 13 '06 #7
rkc
kaniest wrote:
Lyle Fairfield schreef:
Ceiling = -(Int(-d))

Wow! A quote from the Excel 3.0 manual ... ;-)


Everyone learns what they know from somewhere.
Apr 13 '06 #8
That's interesting to know. I got it from:

From: Paul van Goudoever - view profile
Date: Fri, Sep 18 1998 12:00 am
Email: "Paul van Goudoever" <pgoud...@sig.nl>
Groups: comp.databases.ms-access

This one handles reals as well:

Function RoundUp5(varNum As Variant) As Long
On Error Resume Next
If IsNumeric(varNum) Then
RoundUp5 = -Int(-varNum / 5) * 5
End If
End Function

Unfortuantely, Paul doesn't post here anymore.

Apr 13 '06 #9
Lyle Fairfield
Unfortuantely, Paul doesn't post here anymore.


Not as often as I used to but ...

Bye,
Paul
Apr 13 '06 #10
Did Excel 3.0 have VB(A) enabled?

Apr 13 '06 #11
Lyle Fairfield:
Did Excel 3.0 have VB(A) enabled?


IIRC it had some macro language ...
A co-worker gave me the wow-comment when he saw the posting.
Apr 13 '06 #12
Sorry, I wasn't clear. I meant the smart Paul. Well, you could be Paul
VG, I suppose, as I've seen smart things posted by you perviously. If
you were Paul VG you would probably have noted over the years that I
have credited you with this function on several occasions, and forgiven
me for failing to do so in a quickie reponse late at night.

Apr 13 '06 #13
I feel a pretty dumb right now, but I still think I hardly
deserve any credits for -(int(-x)). Maybe a tiny little
bit for first posting it in cdma.
Apr 13 '06 #14
I don't deserve as much credit as that. But it might help Bradley.

Assuming you are Paul VG you haven't posted much that's dumb here,
nothing that I have ever noted. If you have, it's compensated for by a
whole bunch of smart things you have posted.

So let's just forget this exchange and move forward.

Apr 13 '06 #15
"Br@dley" <do***********@google.com> wrote
Ta. I guess my main question was WHY the
Cint() rounds to the nearest even number?
Just interested in the reasoning for imple-
menting it that way.


It's called "Banker's Rounding" and it is what Microsoft chose to use, after
conferring with their customers.

Larry Linson
Microsoft Access MVP
Apr 13 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Steven T. Hatton | last post by:
I'm surprised I haven't hit this situation till now, but I don't believe I've had to deal with it before. I have a function that sets the components of a point class (QPoint from Qt). It takes...
3
by: Caesar Augustus | last post by:
Hello Tom, Long time listener, first time caller... I have been working with vb code to automate a salary increase process that not only rounds to the nearest penny but also rounds to the...
3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
2
by: whojustdenyme | last post by:
Hey guys, I'm new to C and programming..so I need some help. I need a way to round an INPUT number to an INPUT value so for ex: enter a decimal: 3.1459 enter the number to round: 3 Answer:...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
5
by: Yasin cepeci | last post by:
Converting single to int Rounds the upper number if the part after dot is bigger than 0,5. But I want to discard the value after dot.
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.