473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How split a float number out into feet and inches

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
4 12203
D_C
293 100+
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
dush
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
keith
2
to D_C and Dush
Thanks for all the help.
Really appreciate it.

Thanks
Sep 23 '06 #4
dush
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

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

Similar topics

19
by: cover | last post by:
Is there a restriction to the number of fields you can work with in a PHP to MySQL connection? I'd used as many as 15 quite a few times and now I have a form with 34 fields and can't seem to get...
20
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51...
1
by: SpookyTooth | last post by:
I am trying to write a program that will convert a height of a person from feet and inches to centimeters. I want to use the relationship: 1 foot = 30.48 cm and 1 inch = 2.54 cm. I am trying to get...
3
by: SpookyTooth | last post by:
I am trying to write a program that will convert a height of a person from feet and inches to centimeters. I want to use the relationship: 1 foot = 30.48 cm and 1 inch = 2.54 cm. I am trying to get...
3
by: Jason S | last post by:
is there any way to use templates to bind integer/floating point constants to a template for compile-time use? e.g. template <double conversion> class meters { const factor = conversion;
27
by: galt_57 | last post by:
I need to do just a few multiplies of long integers and have a divide by ten. Can I avoid using floats? Can I use two longs as a 64 bit value somehow? Thanks.
6
by: philiprodley | last post by:
Have 3 text boxes. Feet, inches and metres. As I type in feet then inches, I would like to convert this to metres and place the result in the metres text box as soon as I tab on to the next...
7
by: Greatness | last post by:
Ceramic Floor Tile You’re working for a company that lays ceramic floor tile, and they need a program that estimates the number of boxes of tile for a job. A job is estimated by taking the...
2
by: Steve | last post by:
I want to open a window that has been pre-formatted with a table and then write rows to the table. The pre-formatted url is named budget.html and the body looks like this: <body> <h1...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.