views:

8290

answers:

11

How can we extract the decimal part of a floating point number and to store the decimal part and the integer part into seperate two integer variables?

+18  A: 

You use the modf function:

double integral;
double fractional = modf(some_double, &integral);

You can also cast it to an integer, but be warned you may overflow the integer. The result is not predictable then.

Johannes Schaub - litb
+3  A: 

Try this:

int main() {
  double num = 23.345;
  int intpart = (int)num;
  double decpart = num - intpart;
  printf("Num = %f, intpart = %d, decpart = %f\n", num, intpart, decpart);
}

For me, it produces:

Num = 23.345000, intpart = 23, decpart = 0.345000

Which appears to be what you're asking for.

Chris Bunch
the first part is correctbut wish to get the result as decimal part as 345000 integer
Binu
but i want to get the deciaml part as Integer valuewhich means decpart=345000
Binu
int decpart = 100000*(num - intpart);
TokenMacGuy
+1  A: 

I don't see how you could put the decimal part into an integer unless you knew how many digits you wanted to keep. Could you give a few examples?

Nosredna
i want to get the decimal part as integer for any inputting value.so the number of digits in the deciaml part cannot predict.
Binu
like if u input 16.25i want to get 25 in an integer valuealso if teh nuber is 0.3215769i want to get 3215769 in an integer like that
Binu
reading into a string, then checking to see whether the string is a double (questions exist on SO that are answered saying how to do that), and then getting the stuff after the ".". what about that? or do you need to read other formats too (10e-5)?
Johannes Schaub - litb
is thre any othe r method for doing this other than using string manipulation.??
Binu
i didnt meant these format(10e-5).i meant the exact number..
Binu
What if you have a bunch of zeros after the decimal point? They can't be represented unless they are in a string--an int wouldn't work for that.
Nosredna
+1  A: 

Other answers have given you how to split the whole part from the fractional part. To do what you want with the fractional part, just keep multiplying it by 10 until the fractional part of that becomes 0.

You may have to deal with overflow converting that to an integer (if you're working with doubles instead of floats).

Also, I'm not sure how rounding error might screw with this - been a long time since I did numerical analysis. And even then it was pretty much the minimum to get a good grade in some class.

I'll leave those problems and the actual implementation as an exercise for the reader. Is this homework by any chance?

Michael Burr
+2  A: 

If you're reading in from the console, you don't need to ever store it in a float. Just read the two parts in as integers with scanf:

int int_part, dec_part;
scanf("%d.%d", &int_part, &dec_part);
Zach Langley
Again, how do you distinguish 1.5 from 1.005 ?
Robert L
A: 

Well, floor() or casting will get you the integer part easily, but what do you actually expect to store in the "decimal" part?

Not even including issues like floating point (im)precision, what do you want to happen when your candidate float is a repeating fraction or an irrational number?

HUAGHAGUAH
A: 
mrwes
A: 

The quick "in a nut shell" most obvious answer seems like:

#define N_DECIMAL_POINTS_PRECISION (1000) // n = 3. Three decimal points.

float f = 123.456;
int integerPart = (int)f;
int decimalPart = ((int)(f*1000)%1000);

You would change how many decimal points you want by changing the N_DECIMAL_POINTS_PRECISION to suit your needs.

Trevor Boyd Smith
Just for fun. Let's do a thought experiement:The more obfuscated and complex ( see "hard to maintain code" ) solution could go as far as [bit slicing][1] the float or double to get the actual ["integerBits" and "fractionalBits"][2].
Trevor Boyd Smith
I'm not exactly sure of why you would do this... maybe this method would have the advantage of capturing the integer and decimal parts directly without losing any of the precision due to floating point rounding.
Trevor Boyd Smith
Here is some incomplete pseudo code to give you the idea: #define BIT_MASK1 /* not sure */ #define SHIFT /* not sure */ #define BIT_MASK2 /* not sure */ float f = 123.456; uint32_t * tmp = (uint32_t *) int integerPart = (int)f;
Trevor Boyd Smith
int decimalPart = (((*tmp) [1]: http://en.wikipedia.org/wiki/Bit_mask [2]: http://en.wikipedia.org/wiki/Q_(number_format)
Trevor Boyd Smith
A: 

dim n as double' original number dim i as integer' integer portion dim d as double ' decimal portion

i = int(n) d = n - i

' if n = 34.45 ' i = int(n) = 34 ' d = n - i = 34.45 - 34 = .45

A: 

dim n as double

dim i as integer

dim d as double

i = int(n)

d = n - i

if n=34.45

then i=int(n)=34

and d=n-i= 34.45 - 34 = .45

A: 

Interesting, but what about this? casting does not work properly, modf() also does not work in this situation (I just want to take integral part only!)

int main()
{
 double d;
 int i;
 int x;

 d = 10.000;

 for (i=1; i< 20; i++)
 {
  x=(int) (d*100.00);
  printf("%lf --> %lf --> %d\n", d, d*100.00, x);

  d = d+0.001;
 }

 return 0;
}

Here was the output, please read carefully at the line: 10.010000 --> 1001.000000 --> 1000

khanhnd@debian96:~/test$ gcc -Wall -O3 rounding.c
khanhnd@debian96:~/test$ ./a.out
10.000000 --> 1000.000000 --> 1000
10.001000 --> 1000.100000 --> 1000
10.002000 --> 1000.200000 --> 1000
10.003000 --> 1000.300000 --> 1000
10.004000 --> 1000.400000 --> 1000
10.005000 --> 1000.500000 --> 1000
10.006000 --> 1000.600000 --> 1000
10.007000 --> 1000.700000 --> 1000
10.008000 --> 1000.800000 --> 1000
10.009000 --> 1000.900000 --> 1000
10.010000 --> 1001.000000 --> 1000
10.011000 --> 1001.100000 --> 1001
10.012000 --> 1001.200000 --> 1001
10.013000 --> 1001.300000 --> 1001
10.014000 --> 1001.400000 --> 1001
10.015000 --> 1001.500000 --> 1001
10.016000 --> 1001.600000 --> 1001
10.017000 --> 1001.700000 --> 1001
10.018000 --> 1001.800000 --> 1001
Khanh Ngo Duy