How split a float number out into feet and inches

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • keith
    New Member
    • Sep 2006
    • 2

    How split a float number out into feet and inches

    I am writing a simple program to convert mm to feet and inches.
    I have it as follows:
    #include <stdio.h>
    #include <math.h>

    void main()
    {
    int x=11;
    float mm=0;
    float answer=0;

    do
    {
    printf("\nPleas e enter your metric millimeter. - ");
    scanf("%f",&mm) ;
    answer=mm*0.003 2808399;

    printf("\n%.0f mm's is equal to %.2f Ft.\n",mm,answe r);
    x=x--;
    }
    while (x>1);
    }
    Although this does fine to get the feet extracted, I would like to know how to
    get the inches instead of a decimal number, like how to split a number like
    5000mm into 16.4042 FT. The sixteen feet is ok. I'd like to see how to get the
    .4042 turned into 4 13/16" and print that out.

    Thanks, Keith
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    12 inches = 1 foot.
    X inches = .4042 feet.

    X * 1 = .4042 * 12
    X = 4.8504 inches

    Use proportions. In other words, multiply by 12. The integer part is how many inches, the fractional part is also how many inches, but you may want to convert that to something even smaller, or even round.

    Comment

    • dush
      New Member
      • Sep 2006
      • 27

      #3
      Hi

      Here is the code that convert mm to ft. and it shows inches in fractions:

      Code:
      #include <stdio.h>
      #include <math.h>
      #define PRECISION 20
      
      int main() {
      
        float mm;
        float answer;
      
        while(1)
        {
          printf("\nPlease enter your metric millimeter (0 to exit program): ");
          scanf("%f", &mm);
          if (!mm) break;
          
          answer = mm * 0.0032808399;  
          printf("\n%.1f mm's is equal to %.0f Ft.", mm, answer);
        
          answer = answer - (int)answer;
          answer *= 12;       
          printf(" and %d", (int)answer);
          
          answer = answer - (int)answer;
          if (answer)
          {    
            int a, b, i = PRECISION, j = PRECISION;
            float min = 1;
            while (--i) 
            {  
              while (--j)
                if (j>i && (j%i || i==1) && (min >= fabs(answer - (float)i/j))) 
                {          
                 min = fabs(answer - (float)i/j);
                 a = i; 
                 b = j;
                }                    
              j = PRECISION;
            }        
            printf(" %d/%d", a, b);
          }    
          puts(" inches.\n");
        }  
        return 0;
      }
      If you enter 5000mm, program output will be:

      5000.0 mm's is equal to 16 ft. and 4 11/13 inches.

      (not fraction 13/16 as you wrote)

      program that convert it in 16ft. 4 13/16 inches was less precious that this one because fractional part of inches for 5000mm is 0.850394.

      11/13 - 0.850394 = -0.00424 - number is expressed in more precious fraction
      13/16 - 0.850394 = -0.037894

      I set the PRECISION in program to 20 (that means it will made xx/yy fractions of inches for you, so the numbers xx and yy are : 1<=xx<=19 and 2<=yy<=19)
      You can set higher PRECISION if u like.

      enjoy.

      Comment

      • keith
        New Member
        • Sep 2006
        • 2

        #4
        to D_C and Dush
        Thanks for all the help.
        Really appreciate it.

        Thanks

        Comment

        • dush
          New Member
          • Sep 2006
          • 27

          #5
          Hi Faith

          I found some bugs in code I posted above before. But this one here works perfect:

          Code:
          #include <stdio.h>
          #include <math.h>
          #define PRECISION 20 // 5 9 17 33 (if bottom fraction part has to be 2 4 8 16 32 ...)
          
          /*int is2PowN(int x)
          {
            while(!(x%2)) x >>= 1;
            return x==1 ? 1 : 0;
          }*/
          
          int main() {
          
            float mm, inches;  
            int a, b;
            
            while(1)
            {
              printf("\nPlease enter your metric millimeter (0 to exit program): ");
              scanf("%f", &mm);
              if (!mm) break;
               
              a = b = 0;                 
              inches = floor((mm / 25.4) * 1000000  + 0.5)/1000000;
              
              if (inches - floor(inches) < 1.0/(2*(PRECISION-1))) inches = floor(inches);
              else if (ceil(inches) - inches < 1.0/(2*(PRECISION-1))) inches = ceil(inches);
              else        
              {         
                float fraction = inches - floor(inches), min = 1;
                int i = PRECISION, j = PRECISION;      
                inches = floor(inches);      
                
                while (--i) 
                {  
                  for (j = PRECISION-1; j; --j)
                    if (j>i && (j%i || i==1) && (min >= fabs(fraction - (float)i/j))/* && is2PowN(j)*/) 
                    {          
                     min = fabs(fraction - (float)i/j);
                     a = i; 
                     b = j;
                    }                    
                }              
              }                    
              printf("\n%.1f mm's is equal to %.0f Ft. and %d", mm, floor(inches/12), (int)inches%12);       
              if (a || b) printf(" %d/%d", a, b);    
              puts(" inches.\n");
            }    
            return 0;
          }
          If the fractions for inches should look like xx/2 xx/4 xx/8 xx/16 xx/32 just set PRECISION to 33 and move away bounds /* and */ for 2 comments.

          Should work perfect, however If anybody spot some bug in this code let me know please.

          Thanks

          Comment

          Working...