My code sample:
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, BigDecimal.ROUND_HALF_UP);
System.out.println(a+"/"+b+" = "+c);
}
}
The result is:
1/3 = 0
What am I doing wrong?
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.
Please read JDK 13 documentation.
Old answer for JDK 1.5 :
this will give the result as 0.33. Please read the API