TheMatrix
TheMatrix

Reputation: 299

How to separate float into an integer and a fractional part?

I am willing to cast precise operations and for that purpose I need a way to seperate a float number into an integer and a fractional part. Is there any way for this?

Upvotes: 16

Views: 47891

Answers (5)

Ale
Ale

Reputation: 997

Certainly, the use of modf() is the best, more direct way to separate the integer from the fractional part of a floating point number. The only drawback is in case you want the integer part to actually be an integer; for example, if you want to split a time value expressed in seconds. Using modf() you need an intermediate value to store the floating point format of the integer part. An alternative is to use fmod() instead. Here's an example:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>

int main(int argc, char *argv[])
/*
* Convert arguments to double, then split seconds and microseconds.
*/
{
    for (int i = 1; i < argc; ++i)
    {
        double d = atof(argv[i]);
        struct timeval tv;

        tv.tv_sec = trunc(d);
        tv.tv_usec = trunc(fmod(d, 1.0)*1000000.0);

        printf("%g\t-> %ld.%06ld\n", d, tv.tv_sec, tv.tv_usec);
    }
    return 0;
}

Output:

$ ./a.out 123.45678987654321 0.1 0.01 0.001 0.0001 0.00001 0.000001 0.0000001
123.457 -> 123.456789
0.1     -> 0.100000
0.01    -> 0.010000
0.001   -> 0.001000
0.0001  -> 0.000100
1e-05   -> 0.000010
1e-06   -> 0.000001
1e-07   -> 0.000000

Upvotes: 1

Mohamed Mahrous
Mohamed Mahrous

Reputation: 1

#include <bits/stdc++.h>

using namespace std;

int main()
{
    double n;
    cin>>n;
    double fr = n-((int)n);

    cout<<"int "<<(int) n<<"\n";

    cout<<"fraction "<< fr;

    return 0;
}

Upvotes: -1

One other way using type cast.

#include <stdio.h> 
#include <math.h>
void main()
{ 
    float a = 3.4;
    float a_frac = a - (int) a;
    float a_int = a - a_frac;
    printf("Number = %f, Integer = %f, Fraction = %f", a, a_frac, a_int);
}

Upvotes: 2

Edenia
Edenia

Reputation: 2488

There is a function included in math.h library called modf With this function you can do just what are you trying to.

Example:

#include <stdio.h>
#include <math.h>

double ftof ()
{
    double floating = 3.40, fractional, integer;

    fractional = modf(floating, &integer);
    printf ("Floating: %g\nInteger: %g\nFractional: %g", floating, integer, fractional); // when using printf, there are no floats

    return fractional;
}

Output:

Floating: 3.40
Integer: 3
Fractional: 0.40

Note that using double in most of the cases is better than using float, despite that double consumes twice the memory of float (4:8 bytes) hence the increased range and accuracy. Also in case you need more precise output from bigger floating numbers when printing, you can try the printf() exponent format specifier %e instead of %g which only uses the shortest representation of the floating decimal.

Upvotes: 36

Ankit Jaiswal
Ankit Jaiswal

Reputation: 1

A thought crossed my mind to separate them with some logic :

    #include <iostream>

using namespace std;
int main()
    {
    double fr,i,in,num=12.7;
    for(i=0;i<num;i++)
    {
        fr=num-i;
        }
        cout<<"num: "<<num;
cout<<"\nfraction: "<<fr;
in=num-fr;
cout<<"\nInteger: "<<in;
    }

Hope this was what you were searching for:) .

Upvotes: 0

Related Questions