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
Answer from Rohan Grover on Stack OverflowYou 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
There's no ergonomic setting in the API as far as I'm aware, but why not just do:
try {
//do you're thing
} catch(ArithmeticException e) {
//if it fails, then set the scale
x.setScale(...)
}
You need to first check what is the nature of the result of the division. If it is a price, then set the scale to 2 beforehand. If it is a rate (e.g. an exchange rate, an interest rate, etc.) then choose a suitable scale to make your calculations sufficiently precise, and then you can call stripTrailingZeros if you like to make the presentation nicer:
BigDecimal.valueOf(1).divide(BigDecimal.valueOf(3), 8, RoundingMode.HALF_EVEN)
➛ 0.33333333
BigDecimal.valueOf(1).divide(BigDecimal.valueOf(4), 8, RoundingMode.HALF_EVEN)
➛ 0.25000000
BigDecimal.valueOf(1).divide(BigDecimal.valueOf(4), 8, RoundingMode.HALF_EVEN)
.stripTrailingZeros()
➛ 0.25
However, if you are changing the scale purely for presentational reasons, I would prefer to leave the scale at a constant 2 for prices and 8 (or higher) for rates, and then use suitable formatting to suppress trailing zeros when you eventually come to printing out the values:
new DecimalFormat("#.##########").format(new BigDecimal("12.340000000"))
➛ "12.34"
Change
.divide(constant1);
to
.divide(new BigDecimal(constant1));
Btw, why don't you just do something like
int temp = (int) (Math.round(v1.doubleValue()) / constant1);
??
You should write the code as follows:
int temp = BigDecimal.valueOf(v1.longValue())
.divide(BigDecimal.valueOf(constant1)).intValue();
The thing about BigDecimal and BigInteger is that they mostly operate with instances of these classes and not with primitive types. Results of operations are also new instances of these classes.
One other remark: I advise to use valueOf static method instead of a constructor, because by doing this you may have a possibility to avoid creation of new objects. Using a constructor makes creation of a new object explicit, which is not necessary in this case at all.