Hi,

    You are right this is not relevent to servlets !!! :-( (bad boy, sit in the
corner and face the wall!)

However it is the wonders of floating point arithmatic for you This is because .1
cannot be represented exactly as a double (or, for that matter, as a binary
fraction of any finite length).

you could do this however,

import java.math.BigDecimal;

public class Mathtest {

  static public void main(String[] args){
 double a = 0.7;
 double b = 0.6;
 BigDecimal c = new BigDecimal(a).subtract(new BigDecimal(b));
 BigDecimal d = c.setScale(2, BigDecimal.ROUND_HALF_UP);


 System.out.println("a=" + a + " b=" + b + " d=" + d );
    }
}


Karl


Tsao Su Jen wrote:

> Sorry for posting this off-topic question here, but since people in this list
> have most probably coded extensively with Java, I'm hoping somebody can provide
> some insights.  In my java program,  an  expression like the following evaluates
> to false:
>     (0.7 - 0.6 == 0.1)
>
> The following code snipplet:
>      double a = 0.7;
>      double b = 0.6;
>      double c = a - b;
>      System.out.println("a=" + a + " b=" + b + " c=" + c);
> gives the result:
>                a=0.7 b=0.6 c=0.09999999999999998
>
> Why is the result of the above operation not exact?  If this is the expected
> behaviour, what would be a good way to handle such numeric operations and
> comparisons, e.g.  when we're doing data validation?
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to