459,725 Members | 1,048 Online
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

Rounding when converting float to integer

 P: n/a 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 Replies

 P: n/a 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 earthlink net Oakland, CA (USA) Apr 13 '06 #2

 P: n/a Public Function Ceiling(ByVal d As Double) As Long Ceiling = -(Int(-d)) End Function Apr 13 '06 #3

 P: n/a 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

 P: n/a "Br@dley" 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

 P: n/a Br@dley wrote in message : 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

 P: n/a Lyle Fairfield schreef: Ceiling = -(Int(-d)) Wow! A quote from the Excel 3.0 manual ... ;-) -- Paul Apr 13 '06 #7

 P: n/a 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

 P: n/a 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" 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

 P: n/a Lyle Fairfield Unfortuantely, Paul doesn't post here anymore. Not as often as I used to but ... Bye, Paul Apr 13 '06 #10

 P: n/a Did Excel 3.0 have VB(A) enabled? Apr 13 '06 #11

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a "Br@dley" 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