Skip to content

Instantly share code, notes, and snippets.

@reddikih
Created June 14, 2018 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reddikih/5b3ddd8cc03e5c3df7cabca6f2c4acca to your computer and use it in GitHub Desktop.
Save reddikih/5b3ddd8cc03e5c3df7cabca6f2c4acca to your computer and use it in GitHub Desktop.
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