1. Senior Member
Join Date
Jul 2005
Location
Geneva (Switzerland)
Posts
304
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. Senior Member
Join Date
Aug 2006
Location
Arequipa-Peru / South America
Posts
2,793
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

3. Senior Member
Join Date
Aug 2006
Location
Arequipa-Peru / South America
Posts
2,793
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

4. Senior Member
Join Date
Aug 2004
Posts
2,715
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. Senior Member
Join Date
Aug 2006
Location
Arequipa-Peru / South America
Posts
2,793
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,

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

6. Junior Member
Join Date
Feb 2008
Posts
1
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

#### Posting Permissions

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