# Thread: Money Problems ... double dose not work ..

1. Member Join Date
Oct 2010
Posts
23
Rep Power
0

## Money Problems ... double dose not work ..

Hi everyone

Iv done a bit of searching on this subject and i realize that their is a problem with financial calculations using doubles although iv read about bigdecimal and floating point but i am not sure how to implement them for my problem. Actually as I am relatively new to java (and maths) I am not even sure if I am going about this the right way but alas i will try to pick some of ye more experience guys brains.

The way I would describe this problem is

x*? = v*?

with x and v being fixed variables in a financial format e.g 4.50
and ? being the variable im looking for..

so 22.00*m = 10.00 + m
solve for m ..

I have tried rounding doubles to 2 decimal places which works (as im not extremely worried about accuracy) for smaller numbers but when i use larger numbers it dose not work

an example of this is
1.51*? = 10+ ?
returned
1.51*19.51 = 10*19.51 which is ok for my purposes.

I would like to make this work for lets say 54.20*? + 10+?
anyway here is my code so far (for what its worth)
Java Code:
```public class SimpleMain {

public static void main(String[] args) {

for (double i =0.00; i< 100000.00; i =i +0.01) {

double m = 10.00;
double w = 3.03;
double x = roundTwoDecimals(i);
double z =  roundTwoDecimals(w);
double v =  roundTwoDecimals(m);
double q = z *x;
double p =  roundTwoDecimals(q);

if( p == v + x  ) {
System.out.println(x);
}

}```
hope ye guys can help thanks  Reply With Quote

2. ## Instead of using double to represent dollars, use ints to represent cents.  Reply With Quote

3. Member Join Date
Oct 2010
Posts
23
Rep Power
0

## Ok cool thanks Kevin . Just on a few things . Is this convention for simple financial calculations ? And am I ok using this code for outputting and taking input as doubles ( simple casting)

Java Code:
```     double f = 54.45;
int y = 0;
f = f*100;
y = (int) f;```  Reply With Quote

4. Member Join Date
Oct 2010
Posts
23
Rep Power
0

## nope that won do it for me take for instance

55.55*? =10.00*?

5555*1 ??

this is very complicated maybe over my head to be honest  Reply With Quote

5. ## I have no idea what you are saying/asking in your last post.

But the suggestion of working with ints and cents is a good idea when dealing in money. Get rid of all doubles and just use ints. If you get user input such as 12.34 then take it as a String and convert it to an int of 1234.  Reply With Quote

6. Member Join Date
Oct 2010
Posts
23
Rep Power
0

## hi junky . sorry yeah that was a bit brief iv been at this all day and im still very confused.

just a look at the problem again i want to solve for x

5555 * x = 1000 +x

when i break it down into cents if u mul 5555 by 1 it makes the equation useless. I need to mul by a smaller amount but that would bring me back to doubles or floats(which im not sure about using). I have a lecturer that has done some programing in finance so i might have to ask her but i am on a break from college at the moment and this is just a project i am doing myself so it might be in appropriate to send an e mail asking ..  Reply With Quote

7. ## So you are not doing something simple like : if a person has 10 dollars and spends one dollar ninety five cent, how much change do they have? This can easily be done using ints instead of doubles.

You are doing something more complicated and you probably need to use BigDecimal. On the other hand it may not be possible to find a solution to these problems given the inherit inaccuracies of trying to represent floating point numbers in binary. Google "What every computer scientist should know about floating point arithmetic" or something along those lines.  Reply With Quote

8. Member Join Date
Oct 2010
Posts
23
Rep Power
0

## Ok thanks junky appreciate the help  Reply With Quote

#### Posting Permissions

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