0

I have 2 BigDecimals, and I want to remove the same amount leading zeros for both numbers, even those behind the comma. Is there any way of doing that without the expensive toString conversion? Heres some examples of how I want it to look like:

0.0004 -> 0.4

there were 3 zeros behind the comma, so I want to remove 3 zeros on the other one too:

0.00003 -> 0.03

Distjubo
  • 959
  • 6
  • 21
  • possible duplicate of [Format a BigDecimal as String with max 2 decimal digits, removing 0 on decimal part](http://stackoverflow.com/questions/10269045/format-a-bigdecimal-as-string-with-max-2-decimal-digits-removing-0-on-decimal-p) – specializt Sep 04 '15 at 09:21
  • 1
    You mean, you want to multiply the numbers by a power of ten? – Marko Topolnik Sep 04 '15 at 09:24
  • @MarkoTopolnik yes, but both numbers shouldn't be greater than 10. And I have to determine the amount of zeros in both numbers – Distjubo Sep 04 '15 at 09:25

4 Answers4

1

I think you can use movePointRight method. The following is javarepl output.

java> BigDecimal a = new BigDecimal("0.0004");
java.math.BigDecimal a = 0.0004
java> a.movePointRight(3);
java.math.BigDecimal res2 = 0.4
java> BigDecimal b = new BigDecimal("0.00003");
java.math.BigDecimal b = 0.00003
java> b.movePointRight(3);
java.math.BigDecimal res4 = 0.03
ntalbs
  • 28,700
  • 8
  • 66
  • 83
1

Take the larger number, apply movePointRight iteratively while comparing with 0.1 to know when to stop. Then move the point right by the same amount on the smaller number.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

Here a quick proposition

    BigDecimal a = new BigDecimal("0.0004");
    BigDecimal b = new BigDecimal("10000.000003");
    final BigInteger aInteger = a.abs().toBigInteger();
    final BigDecimal aDecimal = a.subtract(new BigDecimal(aInteger));
    final BigInteger bInteger = b.abs().toBigInteger();
    final BigDecimal bDecimal = b.subtract(new BigDecimal(bInteger));
    final BigDecimal x = aDecimal.compareTo(bDecimal) > 1 ? b : a;
    BigDecimal DECIMAL = x.subtract(new BigDecimal(x.abs().toBigInteger()));
    int count = 0;
    final BigDecimal one = new BigDecimal(1);
    while (DECIMAL.multiply(new BigDecimal(10)).compareTo(one) < 0) {
        count++;
        DECIMAL = DECIMAL.multiply(new BigDecimal(10));
    }
    a = new BigDecimal(aInteger).add(aDecimal.movePointRight(count));
    b = new BigDecimal(bInteger).add(bDecimal.movePointRight(count));
    System.out.println("a =" + a + "  b = " + b);

You can do better

Aguid
  • 943
  • 2
  • 10
  • 24
0

Try DecimalFormat, check out the url http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

Play around BigDecimal like below

DecimalFormat nf = new DecimalFormat("##,###.###");
nf.setParseBigDecimal(true);
BigDecimal bd = ((BigDecimal) nf.parse("10,233.907")).setScale(3,BigDecimal.ROUND_HALF_UP);
Balaji Kannan
  • 407
  • 6
  • 24
  • Thanks, but I really don't want to be converting them to a String because that takes some time with those big numbers. (I already have 2 BigDecimals) Thank you anyway. – Distjubo Sep 04 '15 at 09:28