This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
why 1234567891.00000001 == (long)1234567891.00000001 returns true even though they are fals but 123456789.00000001 == (long)123456789.00000001 returns true..
Also, How do i know whether one number is pure decimal or number. What I mean pure decimal number is after point(.) if it has 00001 or some other number is pure decimal. And I want to consider 1000.000 (all 0 after point) as long. How do I make that difference without writing some custom function. Is that possible?
If you remove string and give numbers as double instead of string it says equal(compare method returns 0) BigDecimal bd1 = new BigDecimal(1234567891.00000001); BigDecimal bd2 = new BigDecimal(1234567891); bd1.compareTo(bd2) returns 0. and prints BD1 == BD2
Joined: Dec 31, 2004
What you need to understand, Rizwan my comrade, is that this number 123456789.00000001 cannot be expressed as double, simply because double cannot hold that small precision number. Then it simply discards it the extra precision.
So, if you create the BigDecimal using the same double value that is causing you trouble, you will get the same result back again.
Use Strings to hold the decimal value and then use the compareTo method to determine if one number is bigger, smaller or equal to the other.
Be careful with the equals() method. Read the javadoc API about it. [ July 21, 2006: Message edited by: Edwin Dalorzo ]