# Thread: take off fractionnary part

1. ## take off fractionnary part

I want the operator to take off fractionnary part of a real number
ex 23,345 ------->23

2. >> operator <<

Well, there is no operator for that (or at least not to my knowledge). You'll probably just have to write a function to do that.

--Garfield

3. I think you meant "." instead of "," ... anyway:

float a;
int b;

a = 23.456;
b = (int) a;
a = (float) b;

Problem solved.

/**
*** EDIT:
**/

Actually:

a = (int) a;

That should work.

/**
*** END EDIT.
**/

Quzah.

4. Oh, if you meant "." then quzah is right. I thought you wanted to round off or something.

The reason being is because a float that is typecasted to an int can't support the fractionality of the float so it cuts it off. Just a little insight of why that happens.

--Garfield

5. One way is to use the floor() function in math.h
Code:
```#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
double d = 23.345;

d = floor(d);
printf("d:%lf\n",d);
return 0;
}```

6. You seek Yoda

Or this
-----
#include <math.h>
double modf(double VAL, double *IPART);
float modff(float VAL, float *IPART);

Description

------------

`modf' splits the double VAL apart into an integer part and a
fractional part, returning the fractional part and storing the integer
part in `*IPART'. No rounding whatsoever is done; the sum of the
integer and fractional parts is guaranteed to be exactly equal to VAL.
That is, if . REALPART = modf(VAL, &INTPART); then
``REALPART+INTPART'' is the same as VAL. `modff' is identical, save
that it takes and returns `float' rather than `double' values.
-----

Popular pages Recent additions