Article View: borland.public.jbuilder.java.language
Article #15148Round (fix) of doubles
From: Thomas Christen
Date: Thu, 01 Jul 1999 00:00
Date: Thu, 01 Jul 1999 00:00
22 lines
1154 bytes
1154 bytes
I'm looking for a simple round Methode. It should be able to round a value
(e.g. 37821738912.1345 to 37821738912.135 [up]). I tryed the folowing but
failed :
double round(double value, int scale) {
double factor = Math.pow(10, scale);
double integer = Math.floor( value);
double fraction = ( Math.rint((value - integer) * factor) / factor );
double rounded = integer + fraction;
return rounded;
}
some times it works and and some time it doesent. I observed that every step
above works correct but the last operation (adding the integer- and
fraction-part) leeds some time to funy results like
202.0 + 0.75 = 202.75999999998
Has somone an idea ?
Thanks
Thomas
Technical Details
Message-ID:
<7lfb87$9s6@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,7cf999f6520b6d14,start X-Google-Attributes: gidf74b3,public From: "Thomas Christen" <chr@active.ch> Subject: Round (fix) of doubles Date: 1999/07/01 Message-ID: <7lfb87$9s6@forums.borland.com> X-Deja-AN: 495947129 Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.language X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3