, :
BigDecimal numerator = new BigDecimal(numerator);
BigDecimal denominator = new BigDecimal(denominator);
double result = numerator.divide(denominator).doubleValue();
. double, , .
, n , , .
.
BigDecimal numerator = new BigDecimal(numerator);
BigDecimal denominator = new BigDecimal(denominator);
BigDecimal div = numerator.divide(denominator);
BigDecimal, . 10 , System.out.println(div) 1.
, , numerator denominator
java.lang.ArithmeticException: "Non-terminating decimal expansion; no exact representable decimal result."
, double .
System.out.println(2312 / 2.543);
System.out.println(1.0 / 1.0);
System.out.println(1 / 1);
When using double numbers you can get 0at the end, for example 0.0060in your case. If you want to make sure what you get, you will have to convert your result to Stringusing
String dec = String.valueOf(10.0/10.0);
and then using
String newDec = dec.endsWith("0") ? dec.substring(0, dec.length() - 1) : dec;
to root out the last 0. Of course, if your line ends with .0, you have a choice based on your preference, whether you want to leave this leader .or not.
source
share