17

I am trying to separate a double into the integer and decimal parts

So for example, the number 24.4 should be separated into 24 and 4.

int integer = (int)number;
double decimal = number-(int)number;
System.out.println(decimal); 

Using this method gives me the following values :

integer = 24
decimal = 0.3999999999999986

I need the value of decimal to be 4.

How can this problem be fixed ?

Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125
  • You can format it to print just 1 digit after decimal. – Rohit Jain Oct 24 '12 at 06:41
  • 12
    Do you understand that the number 24.4 can't be exactly represented as a `double`? – Jon Skeet Oct 24 '12 at 06:42
  • possible duplicate of [How do I get whole and fractional parts from double in jsp/java?](http://stackoverflow.com/questions/343584/how-do-i-get-whole-and-fractional-parts-from-double-in-jsp-java) – nneonneo Oct 24 '12 at 17:21

10 Answers10

9

You could do a String split(...). And then Integer parseInt(...) to get back the two integer components.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
6

This is because doubles aren't exactly "real numbers" - remember there are infinite number of real numbers in any range, while there are only finite number of digits in double - thus finite number of values, so some round off must occure.

The fact is, 24.4 cannot be exactly represented by double - so the fraction of your number really is something around 0.3999....
If you want an exact solution - you should use a library that gives you exact values for decimals, such as BigDecimal.

If you want to understand more about this issue of doubles being not exact - you should read more about floating points arithmetics, and this article, though high level, is also a must in order to really understand what's going on.

If you cannot understand these article just yet - just take into consideration: If you need an exact value - doubles cannot provide it - and you should use a library if this is the case.

Community
  • 1
  • 1
amit
  • 175,853
  • 27
  • 231
  • 333
5

Depending on the number of decimal digits, you could use this method:

double number = 24.4;

int integer = (int)number;
double decimal = (10 * number - 10 * integer)/10;

System.out.println(decimal); 

Explanation: Remove the decimal points, do the subtraction, and finally return the decimal point back to its original location!

RGO
  • 4,586
  • 3
  • 26
  • 40
4
float number = (float) 22.45;
int integer = (int)number;
double decimal = number-integer;
System.out.println(integer + "decimal" + decimal); 
  • 3
    Hi user3291532 -- while posting an answer like this can be helpful, it's better to leave some explanation about why this is the correct answer. – Erty Seidohl Apr 05 '18 at 13:58
3

Here is a small method I wrote to accomplish this:

/**
 * 
 * @param n
 * @return 
 */
private static double getFractionalPart(double n) {     
    if (n > 0) {
        return n - Math.floor(n);
    } else {
        return ((n - Math.ceil(n)) * -1);
    }
}
1

Here is my solution: I used a simple method in C to split the Fractional Numbers into two Different Integers.

#include<stdio.h>
void main()
{
float x=24.56; int y=(int)x; float z=(x-y)*1000000000; int zd=(int)z;
printf("%lf",x);
printf("\ninteger Part %d\n",y);
printf("\nDecimal Part %d\n",zd);
}

Output


24.559999
integer Part 24

Decimal Part 559999488
berezovskyi
  • 3,133
  • 2
  • 25
  • 30
Sameer
  • 27
  • 1
1

I figured out the way with little workaround and I am sure it works for all the cases.

The code says it all.

        double number = input.nextDouble(); // 234.025
        String s = Double.toString(number); // "234.025"
        s = "0" + s.substring(s.indexOf(".")); //"0.025"
        double decimal = Double.parseDouble(s);// 0.025
        int num = (int)number; // 234
        System.out.println(decimal + " " + num); //0.025 234
Mahesh Jamdade
  • 17,235
  • 8
  • 110
  • 131
0

First find the number the digits after the decimal point, and with that much number of 10's you have to multiply. eg: x=26.78621 then multiply by 100000[ here you can't multiply like x*10, again x*10 so on (5 times), the 1st time you multiply with 10, it will give you 267.862199999..] After multiplication subtract 2600000 from the result. here is a link of your answer which i have code it. https://stackoverflow.com/a/18517555/2508414

Community
  • 1
  • 1
OnePunchMan
  • 720
  • 15
  • 33
0

As RGO stated, you can do that and also round the decimal number in order to get the appropriate value. Ex:

double number = 20.57;

int integer = (int)number;
double decimal = (10 * number - 10 * integer)/10;
double temp =  Math.round(decimal*100.0)/100.0;
System.out.println("Int = " + integer + "\nDecimal = " + temp);
Alfaza
  • 1
  • 1
0

You can do this:

(val - val.longValue()) * 100

use 1000 to get 3 fractions:

for example:

(1.2445 - 1) * 100 = 0.244
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
nimo23
  • 5,170
  • 10
  • 46
  • 75