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

前端 未结 3 416
情话喂你
情话喂你 2021-01-11 19:36

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:

f         


        
相关标签:
3条回答
  • 2021-01-11 20:07

    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);
    
    0 讨论(0)
  • 2021-01-11 20:08

    As answered earlier, subtracting is the way to go. Just remember to convert the one tenths of feet to inches by multiplying with 12:

    float totalHeight = 5.122222;
    int myFeet = (int) totalHeight;
    float myInches = (totalHeight - myFeet) * 12;
    
    0 讨论(0)
  • 2021-01-11 20:18

    Why don't you use

    CGFloat myInches = totalHeight - myFeet;
    
    0 讨论(0)
提交回复
热议问题