473,216 Members | 1,299 Online

# 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.15000000000000002.

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

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

Regards,

John
Nov 22 '07 #1
5 5640
JosAH
11,448 Expert 8TB
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

kind regards,

Jos
Nov 22 '07 #2
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:

Expand|Select|Wrap|Line Numbers
1. 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.

Expand|Select|Wrap|Line Numbers
1.
2. If foundChange=true;
3.
4.  if( condition ...)
5.  {    foundChange=true;
6.       code....
7. }
8.
9. else
10.   Output (Not enough change)
11.   foundChange=false;
12. }
13.
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=false") 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=false or foundChange=true.

Obviously in another part of the code there is
Expand|Select|Wrap|Line Numbers
1. if(foundChange=true)
2.   code...
3.
4. if (foundChange=false)
5.   code...
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=true)" 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
Nov 23 '07 #3
r035198x
13,262 8TB
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:

Expand|Select|Wrap|Line Numbers
1. 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.

Expand|Select|Wrap|Line Numbers
1.
2. If foundChange=true;
3.
4.  if( condition ...)
5.  {    foundChange=true;
6.       code....
7. }
8.
9. else
10.   Output (Not enough change)
11.   foundChange=false;
12. }
13.
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=false") 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=false or foundChange=true.

Obviously in another part of the code there is
Expand|Select|Wrap|Line Numbers
1. if(foundChange=true)
2.   code...
3.
4. if (foundChange=false)
5.   code...
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=true)" 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
Expand|Select|Wrap|Line Numbers
1. if(foundChange) {
2. //executed if foundChange is true
3. }
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
Nov 23 '07 #4
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:

Expand|Select|Wrap|Line Numbers
1. 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:

Expand|Select|Wrap|Line Numbers
1. x1=0.50+0.20;
2. y1=0.50+0.20;
3. double x= (int)((x1* 100)) / 100.0;
4. 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
Nov 25 '07 #5
JosAH
11,448 Expert 8TB
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):

Expand|Select|Wrap|Line Numbers
1. public double oneTenth(double x) {
2.    return Math.round(10*(x+0.05))/10.0;
3. }
4.
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
Nov 25 '07 #6