15

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?

TheMatrix
  • 189
  • 1
  • 1
  • 8
  • possible duplicate of [getting the fractional part of a double value in integer without loosing precision](http://stackoverflow.com/questions/17259315/getting-the-fractional-part-of-a-double-value-in-integer-without-loosing-precisi) – Sneftel Jun 02 '14 at 12:00
  • 3
    @Sneftel The “duplicate” you have found is about getting the fractional part “in [an] integer”, which is different from obtaining it in the same floating-point type as the original value, and an unrealistic proposition, considering that the fractional part of a `double` can have hundreds of (decimal) digits. – Pascal Cuoq Jun 02 '14 at 12:26

5 Answers5

34

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.

Edenia
  • 2,312
  • 1
  • 16
  • 33
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);
}
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
0

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:) .

0

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
Ale
  • 887
  • 10
  • 14
-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;
}
  • 2
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained. – borchvm Jan 26 '21 at 06:30
  • `` is _not_ a standard header. And `namespace std` is _not_ C. – Toby Speight Nov 09 '22 at 16:52