This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Rounding in java
Posted by Nandita on January 10, 2002 at 6:54 AM
I need to do a rounding in java code in units of 100
eg. If a number is 2762.22 then the o/p should be 2700
Please note that this number is stored either in big decimal object or in a string.
> Your reply was very helpfull I was afraid something like this was happening, do you know an easy way to round off floats?
> > i'm replying to myself after obtaining
> > more information about the subject.
> > this is not a bug, but how IEEE floating
> > point operates.
> > excerpt from a helpful respond :
> > With float and double values, if you want certain rounding behavior,
> > then you must provide for that in your code. The Java language does
> > not do any rounding by default. In fact, the Java Language Specification
> > (JLS) prohibits rounding by default. This surprises many people who are
> > familiar with the rounding done automatically by the C language run-time
> > library.
> > ...
> > This is not a problem with the Java Language.
> > All IEEE floating point arithmetic is inexact. This
> > is the inescapable consequence of storing the results
> > in a finite number of bits (32 for float and 64 for
> > double).
> > You are operating at the limits of IEEE floating point accuracy.
> > As described in the standard: ANSI/IEEE STD 754-1985 (IEEE
> > Standard for Binary Floating-Point Arithmetic), when using
> > double values (which have roughly 18 decimal digits of
> > precision), every calculation introduces an error of about
> > 1.0E-17. For float values (having about 8 decimal digits of
> > precision), every calculation introduces an error of at least
> > 1.0E-7.
> > ----
> > hope this help others that doesn't realize this already.