Results 1 to 8 of 8
  1. #1
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default 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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    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!

  3. #3
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    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;

  4. #4
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    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

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  6. #6
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

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

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  8. #8
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 10-30-2010, 07:27 PM
  2. Work & Earn money from home, use rebate lower price save money $$$
    By arturmoniswork in forum Reviews / Advertising
    Replies: 0
    Last Post: 12-30-2008, 06:57 AM
  3. Using double for money is a sin
    By fishtoprecords in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 08:05 AM

Posting Permissions

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