The actual result is 37.1465555388 whose scale must be 10 for it to be exact.
What the JavaDoc says is that the preferred scale is the difference meaning that if the result didn't actually need to be 10, then it would try to make it 8. For example if you would have divided by 2, whose scale is also 0, the result would have been 18573277.76940000 (scale 8).
EDIT: small addition - you can force the division to a certain scale by using the overloaded divide methods:
divide(BigDecimal, RoundingMode)that will give aBigDecimalwith scale ofthisand value rounded using the specified rounding method if the result would actually need more decimals to be exact.divide(BigDecimal, scale, RoundingMode)that will give aBigDecimalwith specified scale, and value rounded by specified method if needed.
This might be useful if you're dividing by a number you know can cause repeating decimals, like 3 (1/3 = 0.333333...) since, if that happens, the simple divide will throw an exception. Bounding it to a maximum number of decimals will help you avoid the exception but will make your computations less precise.
Answer from Andrei Fierbinteanu on Stack OverflowThe actual result is 37.1465555388 whose scale must be 10 for it to be exact.
What the JavaDoc says is that the preferred scale is the difference meaning that if the result didn't actually need to be 10, then it would try to make it 8. For example if you would have divided by 2, whose scale is also 0, the result would have been 18573277.76940000 (scale 8).
EDIT: small addition - you can force the division to a certain scale by using the overloaded divide methods:
divide(BigDecimal, RoundingMode)that will give aBigDecimalwith scale ofthisand value rounded using the specified rounding method if the result would actually need more decimals to be exact.divide(BigDecimal, scale, RoundingMode)that will give aBigDecimalwith specified scale, and value rounded by specified method if needed.
This might be useful if you're dividing by a number you know can cause repeating decimals, like 3 (1/3 = 0.333333...) since, if that happens, the simple divide will throw an exception. Bounding it to a maximum number of decimals will help you avoid the exception but will make your computations less precise.
These scales are the ones used by the methods which return exact arithmetic results; except that an exact divide may have to use a larger scale since the exact result may have more digits. For example, 1/32 is 0.03125.
public static double divide(double d1, double d2) {
BigDecimal b1 = new BigDecimal(d1);
BigDecimal b2 = new BigDecimal(d2);
return b1.divide(b2, 2, RoundingMode.DOWN).doubleValue();
}
This example returns 105.0000
public class TestBigDecimal {
static BigDecimal decimal = new BigDecimal(1050).setScale(4, BigDecimal.ROUND_HALF_UP);
static BigDecimal ONE_HUNDRED = new BigDecimal(100);
static BigDecimal TEN = new BigDecimal(10);
public static void main(String[] args)
{
BigDecimal decimalResult = decimal.divide(ONE_HUNDRED).multiply(TEN) ;
System.out.println(decimalResult);
}
}
You should adjust the scale during the creation of decimal variable.
You haven't specified a scale for the result. Please try this
2019 Edit: Updated answer for JDK 13. Cause hopefully you've migrated off of JDK 1.5 by now.
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Main {
public static void main(String[] args) {
BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("3");
BigDecimal c = a.divide(b, 2, RoundingMode.HALF_UP);
System.out.println(a + "/" + b + " = " + c);
}
}
Please read JDK 13 documentation.
Old answer for JDK 1.5 :
import java.math.*;
public class x
{
public static void main(String[] args)
{
BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("3");
BigDecimal c = a.divide(b,2, BigDecimal.ROUND_HALF_UP);
System.out.println(a+"/"+b+" = "+c);
}
}
this will give the result as 0.33. Please read the API
import java.math.*;
class Main{
public static void main(String[] args) {
// create 3 BigDecimal objects
BigDecimal bg1, bg2, bg3;
MathContext mc=new MathContext(10,RoundingMode.DOWN);
bg1 = new BigDecimal("2.4",mc);
bg2 = new BigDecimal("32301",mc);
bg3 = bg1.divide(bg2,mc); // divide bg1 with bg2
String str = "Division result is " +bg3;
// print bg3 value
System.out.println( str );
}
}
giving wrong answer