dmilicev

integer_and_decimal_part_of_float_number_v2.c

Sep 15th, 2020
116
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     integer_and_decimal_part_of_float_number_v2.c
  4.  
  5.     How can we extract the decimal part of a floating point number ?
  6.  
  7.     https://stackoverflow.com/questions/499939/how-to-extract-the-decimal-part-from-a-floating-point-number-in-c
  8.  
  9.  
  10.     You can find all my C programs at Dragan Milicev's pastebin:
  11.  
  12.     https://pastebin.com/u/dmilicev
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17. #include<math.h>
  18.  
  19. int main(void)
  20. {
  21.     double x, fractpart, intpart;
  22.     int int_part_x;
  23.  
  24.     x = -8.123456;
  25.     int_part_x = (int)x;
  26.  
  27.     if(x<0)
  28.         fractpart = - x + int_part_x;
  29.     else
  30.         fractpart =   x - int_part_x;
  31.  
  32.     printf("\n %lf \t float number \n", x);
  33.     printf("\n %d \t\t integral part \n", int_part_x);
  34.     printf("\n %lf \t fraction part\n", fractpart);
  35.  
  36.     return 0;
  37.  
  38. } // main()
  39.  
RAW Paste Data Copied