Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: working with BigDecimal and MathContext

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default working with BigDecimal and MathContext

    Hi Every body!!


    I'm learning java this semester, I have a C/C++ background so sometimes things get a litter bit confusing in here, hopefully I learn the very basis soon :-)


    I'm working with a method that accept a double without format, it might looks something like this 14564852.51654874659765456874684639786514356452364 or 784643213218432516548468486.3 and the method must forma it to a two decimal number rounding it up (14564852.52 or 784643213218432516548468486.30)

    this is how I code this method


     public double formatMoney(double money) {
            double money_formatted = 0.00;
     
            if (money >= 0) {
                BigDecimal formatted_1 = new BigDecimal(money);
                MathContext format = new MathContext(2, RoundingMode.CEILING);
                formatted_1.round(format);            
                money_formatted = formatted_1.doubleValue();
                System.out.println(formatted_1);//<-----------------in here I'm getting a big number that I don't know where come from
            }
     
            return money_formatted;
        }


    I'll appreciate any input that any of you can give me!


    have a nice day!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: working with BigDecimal and MathContext

    What are you doing with the value returned by the round() method?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with BigDecimal and MathContext

    I thought the round method modify "this" BigDecimal object, but I'm not sure if I'm doing it correctly, do you know how I should do it????

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: working with BigDecimal and MathContext

    Did you try using the value returned by the round() method?

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with BigDecimal and MathContext

    I just checked it, it returns a BigDecimal object,

    should this work?(I added another BigDecimal object called formatted_2)

     public double formatMoney(double money) {
            double money_formatted = 0.00;
     
            if (money >= 0) {
                BigDecimal formatted_1 = new BigDecimal(money);
                BigDecimal formatted_2;
                MathContext format = new MathContext(2, RoundingMode.CEILING);
                formatted_2 = formatted_1.round(format);            
                money_formatted = formatted_2.doubleValue();
                System.out.println(formatted_1);//<-----------------in here I'm getting a big number that I don't know where come from
            }
     
            return money_formatted;
        }

    thanks again for your inputs


    PS: I don't have my code with me to test it right now :-(

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: working with BigDecimal and MathContext

    should this work?
    The javac and java programs should answer that for you.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with BigDecimal and MathContext

    Quote Originally Posted by Norm View Post
    The javac and java programs should answer that for you.
    it compiles and runs, but doesn't behave I would like to.

    the output that I'm getting is 1500.0 when I introduce 1452.78965412 and I want to get 1452.79,

    what am I doing wrong?

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: working with BigDecimal and MathContext

    Try looking at the round(). It's rounding to the 10th decimal place.

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with BigDecimal and MathContext

    Quote Originally Posted by Mr.777 View Post
    Try looking at the round(). It's rounding to the 10th decimal place.
    round() returns this 1.5E+3.

    I think the error is in this line

    MathContext format = new MathContext(2, RoundingMode.HALF_UP);

    how should I set it? in order to get 1452.79 (only two decimals)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: working with BigDecimal and MathContext

    Try some of the other methods of the BigDecimal class. I'm sure one of them will do what you want.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with BigDecimal and MathContext

    I don't see any other method that can help me :-( , I'm using this site

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: working with BigDecimal and MathContext

    What do the scale methods do?

  13. #13
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with BigDecimal and MathContext

    well, I couldn't (or at least I haven't learn) use scale() method, instead I use setscale() method

     
    public double formatMoney(double money) {
            double money_formatted = 0.00;
     
            if (money >= 0) {
                String dvalue = Double.toString(money);
                BigDecimal bd = new BigDecimal(dvalue, MathContext.UNLIMITED);  //creo que es un poco duro, pero deberķa funcionar
                BigDecimal bd2;
                bd2 = bd.setScale(2, RoundingMode.HALF_UP);
                money_formatted = bd2.doubleValue();       
            }
     
            return money_formatted;
        }


    however this method works fine with any double except those ends by .00

    I introduces 25.00 and then I got 25.0 and I want 25.00 :-(

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: working with BigDecimal and MathContext

    Did you try different rounding modes?

  15. #15
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with BigDecimal and MathContext

    I used FLOOR, CEILING, UP, DOWN, HALF_UP, HALF_DOWN and all gave me the same result

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: working with BigDecimal and MathContext

    That's strange. When I ran it I get the following from different tests:

    System.out.println("f1="+formatted_1 + " f2="+formatted_2 +", mf="+ money_formatted);
    //f1=1452.789654120000022885506041347980499267578125 f2=1.5E+3, mf=1500.0 <<<< round()
    //f1=1452.789654120000022885506041347980499267578125 f2=1452.79, mf=1452.79
    //f1=1452.789654120000022885506041347980499267578125 f2=1452.7897, mf=1452.7897

  17. #17
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with BigDecimal and MathContext

    it is strange, because I get more desirable results this is the code that I'm using right now

    public double formatMoney(double money) {
            double money_formatted = 0.00;
     
            if (money >= 0) {
                String dvalue = Double.toString(money);
                BigDecimal bd = new BigDecimal(dvalue, MathContext.UNLIMITED);  //creo que es un poco duro, pero deberķa funcionar
                BigDecimal bd2;
                bd2 = bd.setScale(2, RoundingMode.HALF_UP);
                money_formatted = bd2.doubleValue();       
            }
     
            return money_formatted;
        }

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: working with BigDecimal and MathContext

    I have no answers other than try different combinations of methods and values until you get the one you want.

  19. #19
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with BigDecimal and MathContext

    ok, thanks very much for trying! :-)

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: working with BigDecimal and MathContext

    You're welcome.

  21. #21
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with BigDecimal and MathContext

    I just learned Java doesn't like 0.00, so it change it to 0.0 so the method works just fine

  22. #22
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: working with BigDecimal and MathContext

    No, you can still use format() to format the Double like 0.00.
    Read through the API Docs once again.

Similar Threads

  1. Break down BigDecimal to individual values
    By tarkal in forum Java Theory & Questions
    Replies: 17
    Last Post: November 16th, 2011, 09:43 AM
  2. not sure why the 'else is not working
    By reddevilggg in forum Loops & Control Statements
    Replies: 3
    Last Post: September 30th, 2011, 03:54 PM
  3. [SOLVED] Working on Win 7 and not on XP
    By shaumux in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 4th, 2011, 05:36 PM
  4. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM