Results 1 to 6 of 6
Thread: java division and decimal error
- 10-07-2009, 04:25 PM #1
java division and decimal error
Hi everyone,
i had a problem with my calculation..below is a sample of my code
Java Code:double ggg = 170000.0 * (1.0 + (10.0/100) ); System.out.println("total :" + ggg );
i was trying to calculate the certain percentage based on the user input value but I got this kind of weird math error...
the expected result should b total : 187,000.0
but the actual result is total : 187,000.00000000003 <----
can help me explain why there is a decimal value is involved?? and how shall I get rid of it ?
Thanks in advance :)Last edited by heartysnowy; 10-07-2009 at 04:29 PM.
- 10-07-2009, 04:28 PM #2
- 10-07-2009, 04:29 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Floating-point arithmetic - JavaWorld
Edit: Far too slow and far too little (too little too late). ;-)
- 10-07-2009, 04:49 PM #4
thanks for the links
ok since that's a binary error, i assume that nothing can be done to solve it....
would it be save to trim them off by using Decimalformat?
- 10-07-2009, 04:56 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
You could also use BigDecimal to round it to the number of places you want.
- 10-07-2009, 04:57 PM #6
Well that depends on what your code is being used for. If it's just some simple calculator for class or being used in some non crucial field(ie !banks, accouting, medicine, professional software) then yes it should be fine.
However if this is going to be used some place where fractions of a cent add up then yes it is important(look at office space and how a fraction of a cent ended up)Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Similar Threads
-
round to two decimal places
By javaMike in forum New To JavaReplies: 3Last Post: 12-24-2011, 02:01 AM -
converting decimal to binary value using recursion in java
By Anindo in forum New To JavaReplies: 3Last Post: 07-25-2009, 01:44 PM -
how to discard remainder on division?
By RobertF in forum New To JavaReplies: 9Last Post: 03-13-2009, 12:20 PM -
decimal calculations?
By arnab321 in forum CLDC and MIDPReplies: 5Last Post: 11-19-2008, 03:36 AM -
Java calculator decimal
By cart1443 in forum New To JavaReplies: 2Last Post: 04-16-2008, 01:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks