# BigDecimal representation problem

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• Jan 16th, 2008, 09:19 AM
gehel
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.
• Jan 16th, 2008, 08:13 PM
dr_pompeii
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
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

• Jan 16th, 2008, 08:28 PM
dr_pompeii
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
• Jan 17th, 2008, 12:45 AM
Andreas Senft
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
• Jan 17th, 2008, 05:36 AM
dr_pompeii
Hi Andreas

Quote:

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,

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 :confused:

regards
• Feb 26th, 2008, 10:12 AM
foridea
Quote:

Originally Posted by Andreas Senft
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
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12