473,216 Members | 1,299 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

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
find the link right now.

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

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
6
by: Anders Würtz | last post by:
i have an assignment to iterate through a collection containing different types of numeric values (float, double, int, byte, short etc.) and to add 1 to all of them. I tried with array and...
0
by: porky008 | last post by:
I need to add a search button to this and have no clue how to do it. Can some one please help me on this? import java.awt.*; import java.awt.event.*; import javax.swing.*; import...
3
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display an array of objects using a GUI. My instructions are that the CD class and it's sublcass don't need to change I just need to modify class CDInventory to include the GUI. I'm not...
30
nexcompac
by: nexcompac | last post by:
I am getting I believe three erros in this code. I had it running perfect untill I added all the restock fee items. This is what I have so far: import java.util.*; // program uses class...
8
nexcompac
by: nexcompac | last post by:
Ok, I am working on an inventory program and this is where I am at thus far. I seem to be getting two errors that I can not figure out. I will post the errors at the end of the code. import...
10
by: Guillermo_Lopez | last post by:
Hello All, I am using VBA in access to perform some calculations. There is a particular sumation that is wrong (barely). this code is withing a loop. TDist = TDist + TempDist Both TDist...
2
by: Canye | last post by:
I have a very long list of codes that i was advised to put in a module but don't know where to start from. Also i have a botton in my software that i what to use to be adding tabs when running my...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.