May 28, 2007 #1 lovekang Programmer Feb 16, 2006 86 KR public static void main(String args[]) { System.out.println(0.1); // 0.1 System.out.println(0.7); // 0.7 System.out.println(0.1 + 0.7); // 0.7999999999999999 } I expect 0.1, 0.7 and 0.8 why this happens? thanks.
public static void main(String args[]) { System.out.println(0.1); // 0.1 System.out.println(0.7); // 0.7 System.out.println(0.1 + 0.7); // 0.7999999999999999 } I expect 0.1, 0.7 and 0.8 why this happens? thanks.
May 29, 2007 #2 prosper Programmer Sep 4, 2001 631 HK //try this System.out.println(0.1f + 0.7f); // 0.7999999999999999 Upvote 0 Downvote
May 29, 2007 Thread starter #3 lovekang Programmer Feb 16, 2006 86 KR Thanks. I'm wondering why this happens? Upvote 0 Downvote
May 29, 2007 #4 Diancecht Programmer Jan 8, 2004 4,042 ES Well, it's an all computing matter: you cannot represent all numbers with a finite number of bytes. http://forum.java.sun.com/thread.jspa?threadID=386078&messageID=1660010 Cheers, Dian Upvote 0 Downvote
Well, it's an all computing matter: you cannot represent all numbers with a finite number of bytes. http://forum.java.sun.com/thread.jspa?threadID=386078&messageID=1660010 Cheers, Dian