Article View: borland.public.jbuilder.java.language
Article #8060Re: How do I round a number?
From: Michael Campbell
Date: Mon, 23 Apr 2001 05:17
Date: Mon, 23 Apr 2001 05:17
20 lines
1660 bytes
1660 bytes
"Yneel" <YneelBoreyl@zfa.pbz> jebgr va <3nr3o004$1_2@qarjf>: > > I would like two divide two numbers and then round up the > fractional part. For example if I divide two ints, 12/7 the > answer is 1.71 but Java will discard the .71 for an answer of > 1 what I would really like to happen is to have it round up to > 2. How do I do this? Thanks Well, to begin with if you're using ints, you're going to get an int result, as you saw. So start using at least one floating point, so you get a floating point return. (Look at the Java promotion rules for how/why this works.) That said, the standard way of rounding (to the nearest unit, for example) is to add 0.5, and take the int result of that. Something like: x = (int)((12.0 / 7.0) + 0.5);
Technical Details
Message-ID:
<Xns908CCDBB2F42michaelscampbellyaho@205.152.0.142>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!news-xfer.nuri.net!enews.sgi.com!newsxfer.eecs.umich.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.atl.bellsouth.net.MISMATCH!newsfeed.atl!news2.atl.POSTED!not-for-mailReferences:
<3ae3b004$1_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,250aeddcad7dba9c X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-04-22 22:24:03 PST Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!news-xfer.nuri.net!enews.sgi.com!newsxfer.eecs.umich.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.atl.bellsouth.net.MISMATCH!newsfeed.atl!news2.atl.POSTED!not-for-mail Newsgroups: borland.public.jbuilder.java.language Subject: Re: How do I round a number? From: michael_s_campbell@yahoo.com (Michael Campbell) References: <3ae3b004$1_2@dnews> Message-ID: <Xns908CCDBB2F42michaelscampbellyaho@205.152.0.142> User-Agent: Xnews/4.04.10 Lines: 20 Date: Mon, 23 Apr 2001 05:17:14 GMT NNTP-Posting-Host: 209.214.21.217 X-Trace: news2.atl 988003034 209.214.21.217 (Mon, 23 Apr 2001 01:17:14 EDT) NNTP-Posting-Date: Mon, 23 Apr 2001 01:17:14 EDT