Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

animefx

macrumors regular
Original poster
May 10, 2005
157
0
Illinois
Here is an implementation of a simple print method I made. I can set it my height from main, but the problem is 5"1 and 5"10 display the same way using %.02f

So if the user type in 5.1 or 5.10 it always display "5.10". Is there a better way of handling this?

- (void) print {

NSLog(@"My age is %i, I weigh %i, and I am %.02f tall", age, weight, height);

}
 
I had to google feet and inches because I come from europe.
But I would advice against using float, because it's not a base-10 metric.

Instead I would code the height as an integer, using 0-15 for the inches (4 bit) and the rest as feet:

4 feet = 16 * 4 + 0 (for the inches)
4 feet, 10 inches = 16 * 4 + 10

the conversion would be:

Code:
int height_from_components(int feet, int inches)
{
   return feet * 16 + inches; // assuming inches are correctly specified as 0-11
}

void components_from_height(int height, int *feet, int *inches)
{
  *feet = height / 16;
  *inches = height % 16;
}

void print_height(int height)
{
   int feet, inches;
   // example usage for above conversion
   components_from_height(height, &feet, &inches);
   printf("Height is %d.%d", feet, inches);
}
 
This will work, thank you very much for your help.

I had to google feet and inches because I come from europe.
But I would advice against using float, because it's not a base-10 metric.

Instead I would code the height as an integer, using 0-15 for the inches (4 bit) and the rest as feet:

4 feet = 16 * 4 + 0 (for the inches)
4 feet, 10 inches = 16 * 4 + 10

the conversion would be:

Code:
int height_from_components(int feet, int inches)
{
   return feet * 16 + inches; // assuming inches are correctly specified as 0-11
}

void components_from_height(int height, int *feet, int *inches)
{
  *feet = height / 16;
  *inches = height % 16;
}

void print_height(int height)
{
   int feet, inches;
   // example usage for above conversion
   components_from_height(height, &feet, &inches);
   printf("Height is %d.%d", feet, inches);
}
 
Feet and inches.

I had to google feet and inches because I come from europe.

Lucky you. Here in the States we're still stuck using the ridiculous "standard" measurement system. I tried for years to go metric, but the way things are run here it's all but impossible. All public records are in feet, inches, miles, etc. Tools, bolts and screws, lumber, all measured in those ridiculous units.

Then there's fractions. Here in the US, carpenters measure in units like 1/4 inch , 1/16, 1/32, and 1/64. Try adding 3 and 3/4 inches to 4 and 7/16. It's awful.

Canada did it right. In Canada, about the time I was in school, they just stopped teaching "standard" units and switched to metric. My parent's generation had to learn a new system, and then they were done.

In the States, the schools give lip-service to the metric system, and teach it as an afterthought. As a result, the switch to metric has been an abysmal failure. It's maddening. It makes us look like provincial idiots.

Math and science is SOOOO much cleaner with metric...

Sigh...
 
So if the user type in 5.1 or 5.10...

If this is a matter of how to handle user input of height, expressed in feet and inches, I would suggest you either find a way to parse the correct values from a string (5' 10", punctuation required) or ask the user for this info as two separate numeric fields (feet, inches).
 
Thank you! This is exactly what I ended up doing. Infact I wrote my own method that has 2 parts. setFeet:setInches

It's amazing how much I've learned in just a few days. Since this post I've written my 1st iPhone application that actually does something useful. I even have it checking for errors and poping up UIAlerts, etc

Tonight I'm writing a class to format and clean up text in a TextView.

I have a lot to learn, but I'm happy with my progress.

If this is a matter of how to handle user input of height, expressed in feet and inches, I would suggest you either find a way to parse the correct values from a string (5' 10", punctuation required) or ask the user for this info as two separate numeric fields (feet, inches).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.