Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BigDecimal Usage Sample
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalTest {
public static void main(String ... args) {
BigDecimalTest app = new BigDecimalTest();
app.test();
}
public void test() {
BigDecimal target = new BigDecimal("12345678");
int p;
BigDecimal divisor;
divisor = new BigDecimal("10000.0"); p = 4;
System.out.printf(
"target: %s, precision: %d, divisor: %.1f, result: %f\n",
target, p, divisor, target.divide(divisor, p,RoundingMode.HALF_EVEN));
divisor = new BigDecimal("10000.0"); p = 3;
System.out.printf(
"target: %d, precision: %d, divisor: %.1f, result: %f\n",
target.intValue(), p, divisor, target.divide(divisor, p,RoundingMode.HALF_EVEN));
divisor = new BigDecimal("10000.0"); p = 2;
System.out.printf(
"target: %d, precision: %d, divisor: %.1f, result: %f\n",
target.intValue(), p, divisor, target.divide(divisor, p,RoundingMode.HALF_EVEN));
divisor = new BigDecimal("100000.0"); p = 2;
System.out.printf(
"target: %d, precision: %d, divisor: %.1f, result: %f\n",
target.intValue(), p, divisor, target.divide(divisor, p,RoundingMode.HALF_EVEN));
divisor = new BigDecimal("1000000.0"); p = 2;
System.out.printf(
"target: %d, precision: %d, divisor: %.1f, result: %f\n",
target.intValue(), p, divisor, target.divide(divisor, p,RoundingMode.HALF_EVEN));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment