Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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
Updated 7-Jul-10 5:47am
v4
Comments
SnowHow 7-Jul-10 13:05pm    
Why avoid the character array/string solution?

This is a very simple way to do this.
C++
#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>
 
Share this answer
 
Comments
CHill60 2-May-14 11:05am    
And Solution 3 gave the same simple solution nearly 4 years ago!
double sample = 1.234
double fractional = sample - Math.Floor(sample);
 
Share this answer
 
Comments
Nish Nishant 10-Jul-10 23:48pm    
Math.Floor? Dude, she's asking a native C++ question :-)
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
 
Share this answer
 
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
 
Share this answer
 
Comments
Nish Nishant 10-Jul-10 23:46pm    
Reason for my vote of 5
Worth 5!
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.
 
Share this answer
 
v2
Comments
nithya.nampoothiri 7-Jul-10 11:24am    
no...i want to do it without converting to text.how it can be performed with integers??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900