S
Stanimir Stamenkov
I've observed depending on whether a BigDecimal is constructed with
a String or double representing the same value, I get different
results in a rounding operation:
import java.math.BigDecimal;
import java.math.RoundingMode;
public class FPTest {
public static void main(String[] args) {
double a = 3.1231215;
BigDecimal b = new BigDecimal(a);
BigDecimal c = new BigDecimal(Double.toString(a));
System.out.println(b.setScale(6, RoundingMode.HALF_UP));
System.out.println(c.setScale(6, RoundingMode.HALF_UP));
}
}
The output I get is:
3.123121
3.123122
How are the two constructors BigDecimal(String) and
BigDecimal(double) different?
a String or double representing the same value, I get different
results in a rounding operation:
import java.math.BigDecimal;
import java.math.RoundingMode;
public class FPTest {
public static void main(String[] args) {
double a = 3.1231215;
BigDecimal b = new BigDecimal(a);
BigDecimal c = new BigDecimal(Double.toString(a));
System.out.println(b.setScale(6, RoundingMode.HALF_UP));
System.out.println(c.setScale(6, RoundingMode.HALF_UP));
}
}
The output I get is:
3.123121
3.123122
How are the two constructors BigDecimal(String) and
BigDecimal(double) different?