Click here to Skip to main content
13,295,051 members (85,034 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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:

int main()
float x;
int a[15],b[15];
cout<<"\nEnter the number:";
int y=x;
float z=(x-y);
cout<<"\nInteger part    :"<<y;
cout<<"\nFractional part :"<<z;
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
SnowHow 7-Jul-10 13:05pm
Why avoid the character array/string solution?
Rate this: bad
Please Sign up or sign in to vote.

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.


Nishant Sivakumar 10-Jul-10 23:46pm
Reason for my vote of 5
Worth 5!
Rate this: bad
Please Sign up or sign in to vote.

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;

Rate this: bad
Please Sign up or sign in to vote.

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.
nithya.nampoothiri 7-Jul-10 11:24am
no...i want to do it without converting to it can be performed with integers??
Rate this: bad
Please Sign up or sign in to vote.

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: bad
Please Sign up or sign in to vote.

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;
CHill60 2-May-14 11:05am
And Solution 3 gave the same simple solution nearly 4 years ago!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171207.1 | Last Updated 1 May 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100