Adding and Displaying Double Values in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JohnSmith70
    New Member
    • Oct 2007
    • 22

    #1

    Adding and Displaying Double Values in Java

    I'm trying to add two values. When adding money it very simple when its 50p+20p=70p. But if I add £1 then I face a problem because 70+1=71 wrong. So I tried to enter £1 as 100p so I have 170p. But here the problem I want to output it as £1.70 instead. So if I divide 170 by 100 I get 1.70

    But here yet another problem. I divided the total by 100. So now when I enter 30p it adds to 1.70+30=31.70 (wrong again).
    So my approach with dividing by 100 didn't work. So I decided to divide the total only if the inserted amount is greater than 100 and the total as well. It stil didn't work.

    So I decided to use double instead. So if I want to insert 50p I would write 0.50. But now this time the problem with this is that when I add 0.50 it is displayed as 0.5 ( I want to display it as 0.50). And in other example, instead of 0.15 the result usually displays something like this eg.0.1500000000 0000002.

    So, how can I make this value (0.150000000000 00002) and display it as 0.15?

    And how can I make this value (0.5) and display it as 0.50?


    Regards,

    John
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You have a couple of options:

    1: all your monetary units should be in pence, i.e. one pound is 100 pence
    2: have a look at the DecimalFormat for display purposes

    and read up on what floating point numbers are all about. I would and could've
    given you a perfect link for that but this forum is a mess at the moment so I can't
    find the link right now.

    kind regards,

    Jos

    Comment

    • JohnSmith70
      New Member
      • Oct 2007
      • 22

      #3
      Thanks for your reply. I've manage to display the money as I want now. One quick question:

      since I'm using the values for calculations and I don't know the problem, can I use this to compare values with d:

      Code:
      double d = (int)((d1 * 100)) / 100.0;
      My code is almost complete. But heres the last part.

      *This is for giving change(money). The vending machine should not dispense the product if the customer has not inserted the exact amount in which the machine will have to give change. And if the machine is to give change it should have the necessary coins that are needed for the change.

      I've been able to make this work for a full refund where nothing is bought and you just need to keep track of the current session.

      Here what I've tried to do with the one I'm stuck on.

      Code:
      If foundChange=true;
      
       if( condition ...)
       {    foundChange=true;
            [I]code....[/I]
      }
      
      else 
      { 
        [I]Output (Not enough change)[/I]
        foundChange=false;
      }
      I'm trying to run a method that at first has a boolean variable that is first false. Then becomes true. When the if conditions within the method are exhausted(all ifs conditions are false) I thought that it would naturally execute the else part. And in this else part, I write the code that makes the boolean from true to false (have "foundChange=fa lse") This I found necessary because only once the conditions ( I have many ifs in this method) has been checked will the system be able to know if it has the necessary coins (hence it can determine if foundChange=fal se or foundChange=tru e.

      Obviously in another part of the code there is
      Code:
      if(foundChange=true)
        [I]code...[/I]
      
      if (foundChange=false)
        [I]code...[/I]
      within the code it calls another method that executes more code.

      Sorry for being ambigous, but I will be submitting this. Anyway whats happening is whichever is above is being executed. I don't understand why. So since here I have put "if (foundChange=tr ue)" on top this gets executed. If I put "if(foundChange =false)" on top this gets executed. I don't get it. Its as if within the if condition it is getting assigned. I tried putting "==" instead still not working.

      I've been able to do the entire question, this is the only one left.If you have a better idea please let me know (here's the problem*)


      Regards,

      John

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JohnSmith70
        Thanks for your reply. I've manage to display the money as I want now. One quick question:

        since I'm using the values for calculations and I don't know the problem, can I use this to compare values with d:

        Code:
        double d = (int)((d1 * 100)) / 100.0;
        My code is almost complete. But heres the last part.

        *This is for giving change(money). The vending machine should not dispense the product if the customer has not inserted the exact amount in which the machine will have to give change. And if the machine is to give change it should have the necessary coins that are needed for the change.

        I've been able to make this work for a full refund where nothing is bought and you just need to keep track of the current session.

        Here what I've tried to do with the one I'm stuck on.

        Code:
        If foundChange=true;
        
         if( condition ...)
         {    foundChange=true;
              [i]code....[/i]
        }
        
        else 
        { 
          [i]Output (Not enough change)[/i]
          foundChange=false;
        }
        I'm trying to run a method that at first has a boolean variable that is first false. Then becomes true. When the if conditions within the method are exhausted(all ifs conditions are false) I thought that it would naturally execute the else part. And in this else part, I write the code that makes the boolean from true to false (have "foundChange=fa lse") This I found necessary because only once the conditions ( I have many ifs in this method) has been checked will the system be able to know if it has the necessary coins (hence it can determine if foundChange=fal se or foundChange=tru e.

        Obviously in another part of the code there is
        Code:
        if(foundChange=true)
          [i]code...[/i]
        
        if (foundChange=false)
          [i]code...[/i]
        within the code it calls another method that executes more code.

        Sorry for being ambigous, but I will be submitting this. Anyway whats happening is whichever is above is being executed. I don't understand why. So since here I have put "if (foundChange=tr ue)" on top this gets executed. If I put "if(foundChange =false)" on top this gets executed. I don't get it. Its as if within the if condition it is getting assigned. I tried putting "==" instead still not working.

        I've been able to do the entire question, this is the only one left.If you have a better idea please let me know (here's the problem*)


        Regards,

        John
        = is for assignment and == is for comparison.
        Note that with booleans you can just do
        [CODE=java]if(foundChange) {
        //executed if foundChange is true
        }[/CODE]

        Why don't you print out the value of foundChange too before the if statement so you see what value it has before executing the if

        Comment

        • JohnSmith70
          New Member
          • Oct 2007
          • 22

          #5
          Urgent Please Help

          Hi, thanks for the replies. I've managed to fix somethings yet I still have a very minor problem, and I'm running out of time.

          I have been able to display the value 0.5000001 as 0.5 using this:

          Code:
          double x1= (int)((x* 100)) / 100.0;
          Yet now even though I have done this, for some reason after certain additions it ends up as a number like 0.79. I thought of doing the math.round function but that would convert it to 1(nearest integer value) instead which I don't want. Can you please give me the formula or code that would convert (e.g. 0.79 to 0.80).

          This is merely an example normally these value are entered from the keyboard with different numbers:

          Code:
          x1=0.50+0.20;
          y1=0.50+0.20;
          double x= (int)((x1* 100)) / 100.0;
          double y= (int)((y1* 100)) / 100.0;
          result:
          x= 0.8
          y=0.79

          I don't get why this is happening. I have two variables with exactly the same formula. Where one works correctly (displaying values like 0.80) the similar y (displays values like 0.79).

          I urgently need your help. Please provide me with the right formula to make (example--> 0.79 into 0.8). I hope there is an easy way to fix this. Because I don't have time to change my variable type from double to something else. I'm hoping there is some type of cast, convert or round-to-the-nearest-decimal point, type of formula to fix .

          I will really appreciate if you can help me out. Waiting for your answer.

          Regards,

          John

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by JohnSmith70
            Yet now even though I have done this, for some reason after certain additions it ends up as a number like 0.79. I thought of doing the math.round function but that would convert it to 1(nearest integer value) instead which I don't want.
            If you want one tenth of accuracy (e.g. 0.79 --> 0.8) simply do the following
            (and still use the round method):

            [code=java]
            public double oneTenth(double x) {
            return Math.round(10*( x+0.05))/10.0;
            }
            [/code]

            Note that the value 0.1. cannot be represented as a binary fraction and the above
            method may be a bit off sometimes; for correct display of your numbers have a
            look at the DecimalFormat class.

            kind regards,

            Jos

            Comment

            Working...