Search code examples
javadecimalbigdecimal

Getting Rid of Decimal place in Java?


I am reading in java inputs in format below

2499.873639
32.374242
0.610059
...

Now i want to get rid of the decimal place and have them in this format

2499873639
32374242
610059
...

I have this code which does it for smaller number for not for larger numbers. The Larger numbers become negative (i think this overflowing) and giving it junk values.

BigDecimal b = new BigDecimal(a).multiply(new BigDecimal("1000000.")

If i increase the 0's by another two

BigDecimal b = new BigDecimal(a).multiply(new BigDecimal("100000000.")

It works for larger numbers but not smaller numbers. In short of having a bunch of if's is there anyway to fix this issue?


Solution

  • Use this :

    BigDecimal b = new BigDecimal(a.toString().replace('.', ''));