473,395 Members | 2,192 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,395 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 5652
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.