Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: BigDecimal representation problem

  1. #11
    Join Date
    Jul 2005
    Location
    Geneva (Switzerland)
    Posts
    304

    Default

    Worst case, you can always extract the needed code from commons-math and adapt it for BigDecimal. It doesnt seem to be too hard to understand and should not be too hard to adapt.

  2. #12
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Exclamation

    Hello gehel

    about of formating string x/y thats done

    the problem is for 14/6 = 2.333.....
    or
    14.00/6.00
    see that this constructor recieve the number, i think already divided
    for my bad luck 2.34
    Code:
    public Fraction(double value,
                    double epsilon,
                    int maxIterations)
             throws FractionConversionException
    i need a method that recieve 2 bigdecimals or 2 doubles to resolve
    the problem about 14 and 6

    Code:
                    BigDecimal a = new BigDecimal("14.1515");
    		Double da = new Double(a.toString());
    		System.out.println("a: "+a+" da: "+da);
    		BigDecimal b = new BigDecimal("6.18");
    		Double db = b.doubleValue();		
    		System.out.println("b: "+b+" db: "+db);
    show
    Code:
    a: 14.1515 da: 14.1515
    b: 6.18 db: 6.18
    c: 2.2899 dc: 2.2899
    so i can play with BigDecimals or doubles

    thanks in advanced
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #13
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    just after to try my last option like playing

    Code:
                   BigDecimal a = new BigDecimal("14.00");
    		Double da = new Double(a.toString());
    		System.out.println("a: "+a+" da: "+da);
    		BigDecimal b = new BigDecimal("6.00");
    		Double db = b.doubleValue();		
    		System.out.println("b: "+b+" db: "+db);
    
    		BigDecimal c = a.divide(b,3); //<--------------- 3 instead of 2
    		Double dc = c.doubleValue();
    		System.out.println("c: "+c+" dc: "+dc);
    show
    Code:
    a: 14.00 da: 14.0
    b: 6.00 db: 6.0
    c: 2.33 dc: 2.33  <---------- the desired value
    so why 14/6 = 2.33333333....
    for
    Code:
    BigDecimal c = a.divide(b,2);
    is 2.34
    and for
    Code:
    BigDecimal c = a.divide(b,3);
    2.33

    it should be the same?

    regards
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  4. #14
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    The int argument of divide() is the rounding-mode which is used to round to the given scale (in your case the scale is 2).
    Rounding mode 2 stands for ROUND_CEILING and 3 for ROUND_FLOOR. So that makes the difference.

    Try out this:
    Code:
    BigDecimal bd = new BigDecimal(2.333d);
    System.out.println(bd.scale(2, RoundingMode.CEILING));
    System.out.println(bd.scale(2, RoundingMode.FLOOR));
    It yields 2.34 and 2.33.

    Regards,
    Andreas

  5. #15
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hi Andreas

    BigDecimal bd = new BigDecimal(2.333d);
    System.out.println(bd.setScale(2, RoundingMode.CEILING));
    System.out.println(bd.setScale(2, RoundingMode.FLOOR));
    show
    Code:
    2.34
    2.33
    thats right

    but in the sun forums, i recieved the same suggestion,

    about RoundingMode.CEILING

    in
    http://java.sun.com/j2se/1.5.0/docs/...e.html#CEILING

    Code:
    CEILING
     
    public static final RoundingMode CEILING
     
        Rounding mode to round towards positive infinity. If the result is positive, behaves as for RoundingMode.UP; if negative, behaves as for RoundingMode.DOWN. Note that this rounding mode never decreases the calculated value.
     
        Example:
        Input Number 	Input rounded to one digit
        with CEILING rounding
        5.5 	6
        2.5 	3
        1.6 	2
        1.1 	2
        1.0 	1
        -1.0 	-1
        -1.1 	-1
        -1.6 	-1
        -2.5 	-2
        -5.5 	-5
    according to this (no tested yet) 0.33..... would become 1

    ant i can remember that this behaviour happens, thats the reason i avoid it, coz all my control of historial of prices gone wrong with this behaviour

    now i am really wondered and confused why in your case it works

    regards
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  6. #16

    Default

    Quote Originally Posted by Andreas Senft View Post
    The int argument of divide() is the rounding-mode which is used to round to the given scale (in your case the scale is 2).
    Rounding mode 2 stands for ROUND_CEILING and 3 for ROUND_FLOOR. So that makes the difference.

    Try out this:
    Code:
    BigDecimal bd = new BigDecimal(2.333d);
    System.out.println(bd.scale(2, RoundingMode.CEILING));
    System.out.println(bd.scale(2, RoundingMode.FLOOR));
    It yields 2.34 and 2.33.

    Regards,
    Andreas

    thanks the knowledge became useful

Posting Permissions

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