Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS objective-C: using modulo on a float to get "inches" from Feet

I am trying to make a simple objective-C height converter. The input is a (float) variable of feet, and I want to convert to (int) feet and (float) inches:

float totalHeight = 5.122222;
float myFeet = (int) totalHeight; //returns 5 feet
float myInches = (totalHeight % 12)*12; //should return 0.1222ft, which becomes 1.46in

However, I keep getting an error from xcode, and I realized that the modulo operator only works with (int) and (long). Can someone please recommend an alternative method? Thanks!

like image 873
jake9115 Avatar asked May 06 '13 07:05

jake9115


2 Answers

Even modulo works for float, use :

fmod()

You can use this way too...

float totalHeight = 5.122222;
float myFeet = (int) totalHeight; //returns 5 feet
float myInches = fmodf(totalHeight, myFeet);
NSLog(@"%f",myInches);
like image 80
Anoop Vaidya Avatar answered Oct 05 '22 09:10

Anoop Vaidya


Why don't you use

CGFloat myInches = totalHeight - myFeet;
like image 44
sunkehappy Avatar answered Oct 05 '22 10:10

sunkehappy