The reason is that floating point numbers only approximate real numbers.
But this should work if all you want is two decimal places:
  (*Chris*)

public class Junk {

  public static void main (String[] args) {
    float d1 = 99.44f;
    float d2 = 99.40f;
    float d3 = (float)(Math.rint((d1 - d2) * 100.00) / 100.00);
    System.out.println(d3);
  } //main

}

----- Original Message -----
From: "lancelot" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, June 29, 2001 1:41 AM
Subject: [JSP-INTEREST] Need your help one float and double calc.


> Dear all,
>       Who can tell me why the result is 0.040000916? and why it's not =
> 0.04, if I want to get 0.04 how could I do? thanks
>
> Lancelot
>
> D:\test>type testD.java
> public class testD
> {
>   public static void main(String args[])
>   {
>     float d1=3D99.44f;
>     float d2=3D99.40f ;
>     float d3 =3D d1-d2 ;
>     System.out.println(d3);
>   }
> }
>
>
> D:\test>java testD
> 0.040000916
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to