Results 1 to 8 of 8
 02142011, 07:58 PM #1Member
 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); } }
 02142011, 07:59 PM #2
Instead of using double to represent dollars, use ints to represent cents.
How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 02142011, 08:20 PM #3Member
 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;
 02152011, 12:21 AM #4Member
 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
 02152011, 12:27 AM #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.
 02152011, 12:48 AM #6Member
 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 ..
 02152011, 01:16 AM #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.
 02152011, 01:34 AM #8Member
 Join Date
 Oct 2010
 Posts
 23
 Rep Power
 0
Similar Threads

i created a jar file.but when i double click it doesnot work
By renu in forum New To JavaReplies: 1Last Post: 10302010, 07:27 PM 
Work & Earn money from home, use rebate lower price save money $$$
By arturmoniswork in forum Reviews / AdvertisingReplies: 0Last Post: 12302008, 06:57 AM 
Using double for money is a sin
By fishtoprecords in forum New To JavaReplies: 3Last Post: 11282008, 08:05 AM
Bookmarks