12,634,107 members (23,348 online)
Rate this:
See more:
Hi,

I want to seperate the integral part and fractional part of a real number.
That is if 56.345 is entered the int part will be shown as 56 and frctional part as 345. Here is the code I tried:

```#include<iostream.h>
#include<conio.h>
int main()
{
float x;
int a[15],b[15];
clrscr();
cout<<"\nEnter the number:";
cin>>x;
int y=x;
float z=(x-y);
cout<<"\nInteger part    :"<<y;
cout<<"\nFractional part :"<<z;
getch();
return 0;
}```

The output is as follows...

```case 1:
Enter the number:24.41

Integer part    :24
Fractional part :0.41

case 2:
Enter the number:56.345

Integer part    :56
Fractional part :0.345001```

In the second case I can't obtain the correct answer. I can solve this by using a char array but I dont want to use that method.

(Edited to make the text look like English and not an Ali-G wet dream).
Posted 7-Jul-10 5:58am
Updated 7-Jul-10 6:47am
Aescleal33.3K
v4
SnowHow 7-Jul-10 13:05pm

Why avoid the character array/string solution?

Rate this:

## Solution 2

Your problem is that the computer can't exactly represent 56.345 and ends up storing it as 56.3450001. It's an unfortunate fact of life that it takes an infinite number of bits to store a arbitrary decimal number in binary.

(Actually it takes an infinite number of decimal digits as well, but that's another story).

The quickest way around this is to convert the number to fix point, which you can store in an integer:

```double x = 0.0;
std::cin >> x;
int y  = int( x * 100000 );

std::cout << "Integral part:   " << y / 100000 << std::endl
<< "Fractional part: " << y % 100000 << "/10000" << std::endl;```

This won't work with big numbers that won't fit in an integer. Multiplying by 10,000 for example makes about half the bits in the integer be used for holding the fractional part so you loose even more range. Ultimately you've got to work out what range and inaccuracy are acceptable to you and go with it.

Cheers,

Ash
Nishant Sivakumar 10-Jul-10 23:46pm

Reason for my vote of 5
Worth 5!
Rate this:

## Solution 3

To get the integer part convert to `int`, the fractional part is number - integer part, to round it use the iostream manipulators (or C printf() functions):

```#include <iostream>
#include <iomanip>
int main()
{
float f=1;
while (f)
{
std::cin >> f;
int i = (int)f;
std::cout << i << '\t' << std::setprecision(3) << f - i << std::endl;
}
return 0;
}```

cheers,
AR
Rate this:

## Solution 1

Convert it to text, split at the decimal and convert back to numbers.

Quite simple really.

All you need to do is remove the back end after X significant figures.
v2
nithya.nampoothiri 7-Jul-10 11:24am

no...i want to do it without converting to text.how it can be performed with integers??
Rate this:

## Solution 4

```double sample = 1.234
double fractional = sample - Math.Floor(sample);```
Nishant Sivakumar 10-Jul-10 23:48pm

Math.Floor? Dude, she's asking a native C++ question :-)
Rate this:

## Solution 5

This is a very simple way to do this.
```#include <stdio.h>
#include <stdlib.h>

int main()
{
float f, t;
int i;
printf("Enter a floating number: ");
scanf("%f", &f);
i = (int)f;
printf("Integer part    :%d\n", i);
t = f-i;
printf("Fractional part :%f", t);
return 0;
}

</stdlib.h></stdio.h>```
CHill60 2-May-14 11:05am

And Solution 3 gave the same simple solution nearly 4 years ago!

Top Experts
Last 24hrsThis month
 OriginalGriff 320 Peter Leow 190 CHill60 105 CPallini 100 Jochen Arndt 80
 OriginalGriff 2,324 ppolymorphe 1,282 Peter Leow 1,004 John Simmons / outlaw programmer 725 CPallini 641