By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

How split a float number out into feet and inches

P: 2
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("\nPlease enter your metric millimeter. - ");
scanf("%f",&mm);
answer=mm*0.0032808399;

printf("\n%.0f mm's is equal to %.2f Ft.\n",mm,answer);
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
Sep 22 '06 #1
Share this Question
Share on Google+
4 Replies


100+
P: 293
D_C
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.
Sep 22 '06 #2

P: 27
Hi

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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define PRECISION 20
  4.  
  5. int main() {
  6.  
  7.   float mm;
  8.   float answer;
  9.  
  10.   while(1)
  11.   {
  12.     printf("\nPlease enter your metric millimeter (0 to exit program): ");
  13.     scanf("%f", &mm);
  14.     if (!mm) break;
  15.  
  16.     answer = mm * 0.0032808399;  
  17.     printf("\n%.1f mm's is equal to %.0f Ft.", mm, answer);
  18.  
  19.     answer = answer - (int)answer;
  20.     answer *= 12;       
  21.     printf(" and %d", (int)answer);
  22.  
  23.     answer = answer - (int)answer;
  24.     if (answer)
  25.     {    
  26.       int a, b, i = PRECISION, j = PRECISION;
  27.       float min = 1;
  28.       while (--i) 
  29.       {  
  30.         while (--j)
  31.           if (j>i && (j%i || i==1) && (min >= fabs(answer - (float)i/j))) 
  32.           {          
  33.            min = fabs(answer - (float)i/j);
  34.            a = i; 
  35.            b = j;
  36.           }                    
  37.         j = PRECISION;
  38.       }        
  39.       printf(" %d/%d", a, b);
  40.     }    
  41.     puts(" inches.\n");
  42.   }  
  43.   return 0;
  44. }
  45.  
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.
Sep 23 '06 #3

P: 2
to D_C and Dush
Thanks for all the help.
Really appreciate it.

Thanks
Sep 23 '06 #4

P: 27
Hi Faith

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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define PRECISION 20 // 5 9 17 33 (if bottom fraction part has to be 2 4 8 16 32 ...)
  4.  
  5. /*int is2PowN(int x)
  6. {
  7.   while(!(x%2)) x >>= 1;
  8.   return x==1 ? 1 : 0;
  9. }*/
  10.  
  11. int main() {
  12.  
  13.   float mm, inches;  
  14.   int a, b;
  15.  
  16.   while(1)
  17.   {
  18.     printf("\nPlease enter your metric millimeter (0 to exit program): ");
  19.     scanf("%f", &mm);
  20.     if (!mm) break;
  21.  
  22.     a = b = 0;                 
  23.     inches = floor((mm / 25.4) * 1000000  + 0.5)/1000000;
  24.  
  25.     if (inches - floor(inches) < 1.0/(2*(PRECISION-1))) inches = floor(inches);
  26.     else if (ceil(inches) - inches < 1.0/(2*(PRECISION-1))) inches = ceil(inches);
  27.     else        
  28.     {         
  29.       float fraction = inches - floor(inches), min = 1;
  30.       int i = PRECISION, j = PRECISION;      
  31.       inches = floor(inches);      
  32.  
  33.       while (--i) 
  34.       {  
  35.         for (j = PRECISION-1; j; --j)
  36.           if (j>i && (j%i || i==1) && (min >= fabs(fraction - (float)i/j))/* && is2PowN(j)*/) 
  37.           {          
  38.            min = fabs(fraction - (float)i/j);
  39.            a = i; 
  40.            b = j;
  41.           }                    
  42.       }              
  43.     }                    
  44.     printf("\n%.1f mm's is equal to %.0f Ft. and %d", mm, floor(inches/12), (int)inches%12);       
  45.     if (a || b) printf(" %d/%d", a, b);    
  46.     puts(" inches.\n");
  47.   }    
  48.   return 0;
  49. }
  50.  
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
Sep 24 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.