Results 1 to 7 of 7
Thread: Addition Problem
- 12-06-2008, 07:41 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
-
you're doing nothing wrong. Please search and read up on floating point numeric representation and digital computers. Note that this is not a Java issue, but a general digital computation issue.
- 12-06-2008, 08:49 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
OK thanks, I thought it was something to do with this, so is there any way to solve this problem?
- 12-06-2008, 09:40 PM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
No real solution to solve. You can round the value or you can create some kind of fraction class if it's that important.
-
For me, the problem is not so much with the number itself but with the display of the number as a String. Often the solution is to format the String so that it's to your liking, and two common ways to do this is with String.format (or printf), or by using a DecimalFormat object. For instance:
Java Code:import java.text.DecimalFormat; public class Fubar { public static void main(String[] args) { double test = 0.05 + 0.1; System.out.println(test); System.out.println(String.format("%.2f", test)); System.out.printf("%.2f%n", test); DecimalFormat df = new DecimalFormat("0.00"); System.out.println(df.format(test)); } }
- 12-06-2008, 11:33 PM #6
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Thanks!
Is df.format parsing the double into a String, the same as Double.toString() say but rounded off?
I've read around and seen articles saying don't use float/double for money, so what type is actually reccomended?
Thanks for the solution anyway it's appreciated, I went with String.format!Last edited by tornado; 12-06-2008 at 11:35 PM.
-
In general you should not use float for much of anything as it has a lower accuracy than double, but neither are very good for money, much for the reason shown in your initial post. They are not 100% accurate, and when dealing with money, especially large amounts, you really need 100% accuracy. fishtoprecords, a very knowledgeable regular here, will tell you that you should use a number not subject to rounding errors seen with float or double. Instead consider using decimal numeric types such as int, long, or BigDecimal or BigInteger. Money is often represented numerically by moving the decimal point over to the right two spaces so that there is no decimal component. Thus 100.00 dollars becomes 10000 cents. Then do all the calculations with cents and only convert back to dollars when displaying the results.
Similar Threads
-
jTextField Simpe Addition - how to ? - Netbeans 6.0
By jesicapalma in forum NetBeansReplies: 4Last Post: 05-24-2010, 09:21 AM -
string addition??
By j2vdk in forum New To JavaReplies: 2Last Post: 09-03-2008, 03:41 PM -
Binary Addition
By Deo Favente in forum Advanced JavaReplies: 11Last Post: 04-24-2008, 05:34 AM -
Addition program that displays the sum of two numbers
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks