dmilicev

integer_and_decimal_part_of_float_number_v3.c

Sep 15th, 2020
161
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     integer_and_decimal_part_of_float_number_v3.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 <stdlib.h>
  18.  
  19. int main(void)
  20. {
  21.     char s_value[60], s_integral[60], s_fractional[60];
  22.     int i, found = 0, count = 1, integral, fractional;
  23.  
  24.     sprintf(s_value,"-8.123456");
  25.  
  26.     for (i = 0; s_value[i] != '\0'; i++)
  27.     {
  28.         if (!found)
  29.         {
  30.             if (s_value[i] == '.')
  31.             {
  32.                 found = 1;
  33.                 s_integral[i] = '\0';
  34.                 continue;
  35.             }
  36.             s_integral[i] = s_value[i];
  37.             count++;
  38.         }
  39.         else
  40.             s_fractional[i - count] = s_value[i];
  41.     }
  42.     s_fractional[i - count] = '\0';
  43.  
  44.     integral = atoi(s_integral);
  45.     fractional = atoi(s_fractional);
  46.  
  47.     printf("\n %s \t float number \n", s_value);
  48.     printf("\n %d \t\t integral part \n", integral);
  49.     printf("\n %d \t fraction part\n", fractional);
  50.  
  51.     return 0;
  52.  
  53. } // main()
  54.  
RAW Paste Data Copied