1

Is there any method to remove the . in java into a double value?

Example :

56.11124

to

5611124
azro
  • 53,056
  • 7
  • 34
  • 70
user6308605
  • 693
  • 8
  • 26

5 Answers5

5

I don't think there's a mathematical way to find out how many decimals there are in a double. You can convert to a String, replace the dot, and then convert it back:

Double.parseDouble(Double.toString(56.11124).replace(".", ""));

Be careful of overflows when you parse the result though!

marstran
  • 26,413
  • 5
  • 61
  • 67
1

This might work

class Example {
    public static void main(String[] args) {
        double val = 56.1112;
        while( (double)((int)val) != val )
        {
            val *= 10;
        }

        System.out.printf( "%.0f", val );
    }
}

Output: 561112

This works by casting the double to int which truncates the floating information 56.11124 => 56. While the values aren't equal you multiply it by the base to push the . out. I don't know if this is the best way.

jakedipity
  • 890
  • 8
  • 19
  • this outputs `56` but the OP wants `5611124` so this is no helping answer at all... – Lino Jul 25 '17 at 08:48
  • @Lino at what point does the above code snippet print out 56? I went ahead and made a full working example in case it's not obvious. – jakedipity Jul 25 '17 at 08:52
1

You can convert to BigDecimal and use the unscaledValue method:

BigInteger unscaled = new BigDecimal(myDouble).unscaledValue();

Depending on your intended output, you might also use BigDecimal#valueof(double) to create the intermediate BigDecimal.

Javadoc for BigDecimal#new(double)

Javadoc for BigDecimal#valueOf(double)

Javadoc for BigDecimal#unscaledValue()

kewne
  • 2,208
  • 15
  • 11
  • Be careful when using the double constructor of big decimal as the javadoc states it can be unpredictable... – Lino Jul 25 '17 at 08:50
  • That's true but only when using literal values (`new BigDecimal(0.1)`). If using with a preexisting variable it should work (`new BigDecimal(myDouble)`) – kewne Jul 25 '17 at 08:55
  • it doesn't ;) try following snippet, it prints true. `double d = 0.1; BigDecimal bigDecimal = new BigDecimal(d); System.out.println(bigDecimal.doubleValue() == 0.1000000000000000055511151231257827021181583404541015625);` – Lino Jul 25 '17 at 08:57
  • `System.out.println(0.1 == 0.1000000000000000055511151231257827021181583404541015625);` also prints true... The difference has to do with the usage of [Double.toString](http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#toString(double)), which uses "at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double.". What this means is that it prints "prettier" versions of values. – kewne Jul 25 '17 at 09:16
1

Here's one way to do it,

First, convert the double to a string. Then, call replace to replace . with an empty string. After that, parse the result into an int:

double d = 5.1;
String doubleString = Double.toString(5.1);
String removedDot = doubleString.replace(".", "");
int result = Integer.parseInt(removedDot);

Obviously, this wouldn't work if the double's string representation is in scientific notation like 5e16. This also does not work on integral double values, like 5, as its string representation is 5.0.

doubles are inaccurate by nature. 5 and 5.0 are the same value. This is why you can't really do this kind of operation. Do you expect different results for a and b?

double a = 5;
double b = 5.0;

If you do, then you can't really do this, since there is no way of knowing what the programmer wrote exactly at runtime.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • be vary that scientific notation just converts to a value behind the scences. `5e3` is equal to `5000`. So your answer works fine even for scientific notation – Lino Jul 25 '17 at 08:54
  • @Lino No, for larger numbers, `toString` makes it into scientific notation. – Sweeper Jul 25 '17 at 08:55
-1

You can convert it to a String and remove the . and convert it back to double something like

double value = 56.11124;
value = Double.valueOf(("" + value).replace(".", "")).doubleValue();

This will return 5611124.0 since its a double you will have the floating point. You can convert it to an int, but you have to take care of the possible overflow. Otherwise it would look like this

int normalized = (int) value;
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107