take off fractionnary part

This is a discussion on take off fractionnary part within the C Programming forums, part of the General Programming Boards category; I want the operator to take off fractionnary part of a real number ex 23,345 ------->23...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1

    Unhappy take off fractionnary part

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

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> 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
    1978 Silver Anniversary Corvette

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    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
    1978 Silver Anniversary Corvette

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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 subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. how to get domain part from URL
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 07-23-2008, 12:06 PM
  3. How to get a part of a string from a point onwards
    By pseudonoma in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 04:09 PM
  4. Replies: 9
    Last Post: 07-11-2006, 04:28 AM
  5. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 12:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21