Article View: borland.public.jbuilder.java.api
Article #2537Re: Simple problem for float calculation
From: Joi Ellis
Date: Tue, 17 Oct 2000 00:00
Date: Tue, 17 Oct 2000 00:00
29 lines
1452 bytes
1452 bytes
eLe <petruschan@realink.com.hk> wrote:
> I have Java program simple as :
> float a = 27.1
> float b = 0.2
> float c = a + b
> c = 27.300001
> I want the ans is c = 27.3 is not 27.300001
> How to solve this problem ? thank you !
Decimals like 0.1 are irrational numbers in binary. You will see
round off errors like this, they are unavoiable.
If you're doing math with money, express the money values in pennies, using
integers variables.
Otherwise, you must change your expression to round the result to the desired
number of significant digits yourself.
--
Joi Ellis Software Engineer
Aravox Technologies joi@aravox.com, gyles19@visi.com
No matter what we think of Linux versus FreeBSD, etc., the one thing I
really like about Linux is that it has Microsoft worried. Anything
that kicks a monopoly in the pants has got to be good for something.
- Chris Johnson
Technical Details
Message-ID:
<8shoqj$n2q16@bornews.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<39ec3576_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,9bcc96b038465cb3
X-Google-Attributes: gid1089d0,public
From: Joi Ellis <joi@joi.aravox.com>
Subject: Re: Simple problem for float calculation
Date: 2000/10/17
Message-ID: <8shoqj$n2q16@bornews.borland.com>
X-Deja-AN: 682491326
References: <39ec3576_2@dnews>
Organization: Another Netscape Collabra Server User
User-Agent: tin/1.4.2-20000205 ("Possession") (UNIX) (Linux/2.2.14-5.0 (i686))
Newsgroups: borland.public.jbuilder.java.api