Results 1 to 6 of 6
  1. #1
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default 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 05:29 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Floating-point arithmetic - JavaWorld

    Edit: Far too slow and far too little (too little too late). ;-)

  4. #4
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    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?

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    You could also use BigDecimal to round it to the number of places you want.

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 03:01 AM
  2. Replies: 3
    Last Post: 07-25-2009, 02:44 PM
  3. how to discard remainder on division?
    By RobertF in forum New To Java
    Replies: 9
    Last Post: 03-13-2009, 01:20 PM
  4. decimal calculations?
    By arnab321 in forum CLDC and MIDP
    Replies: 5
    Last Post: 11-19-2008, 04:36 AM
  5. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 02:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •