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.
Printable View
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.
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
i need a method that recieve 2 bigdecimals or 2 doubles to resolveCode:public Fraction(double value,
double epsilon,
int maxIterations)
throws FractionConversionException
the problem about 14 and 6
showCode: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);
so i can play with BigDecimals or doublesCode:a: 14.1515 da: 14.1515
b: 6.18 db: 6.18
c: 2.2899 dc: 2.2899
thanks in advanced
just after to try my last option like playing
showCode: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);
so why 14/6 = 2.33333333....Code:a: 14.00 da: 14.0
b: 6.00 db: 6.0
c: 2.33 dc: 2.33 <---------- the desired value
for
is 2.34Code:BigDecimal c = a.divide(b,2);
and for
2.33Code:BigDecimal c = a.divide(b,3);
it should be the same?
regards
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:
It yields 2.34 and 2.33.Code:BigDecimal bd = new BigDecimal(2.333d);
System.out.println(bd.scale(2, RoundingMode.CEILING));
System.out.println(bd.scale(2, RoundingMode.FLOOR));
Regards,
Andreas
Hi Andreas
showQuote:
BigDecimal bd = new BigDecimal(2.333d);
System.out.println(bd.setScale(2, RoundingMode.CEILING));
System.out.println(bd.setScale(2, RoundingMode.FLOOR));
thats rightCode:2.34
2.33
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
according to this (no tested yet) 0.33..... would become 1Code: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
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