1

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?

smushi
  • 701
  • 6
  • 17

4 Answers4

0

Use this :

BigDecimal b = new BigDecimal(a.toString().replace('.', ''));
afzalex
  • 8,598
  • 2
  • 34
  • 61
-1
String formattedInput = (String.valueOf(input)).replace(".", "");
Tetramputechture
  • 2,911
  • 2
  • 33
  • 48
-1

Convert the double to String, if it is not.
Then use String.replace('.', '');
Then Convert back to int or long, if necessary.

-1

You can do this with String replacement functions.

public static BigDecimal noDecimal(BigDecimal b) {
    return new BigDecimal(b.toPlainString().replace(".", ""));
}

If you already have a String rather than a BigDecimal, this can be simplified to this:

public static BigDecimal noDecimal(String s) {
    return new BigDecimal(s.replace(".", ""));
}
Pokechu22
  • 4,984
  • 9
  • 37
  • 62