Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Function:</strong></p> <pre><code>- (NSString *)theFunction:(float)input { NSArray * array = [NSarray initWithObjects:nil,nil@"1/8",nil,@"1/4",] int fractions = lroundf((input - (int)input)/((float)1/(float)16)); if(fractions == 0 || fractions == 16) { return [NSString stringWithFormat:@"%d",lroundf(input)]; } else { return [NSString stringWithFormat:@"%d %d/16",(int)input,fractions]; } } </code></pre> <p><strong>Note:</strong></p> <p>The <code>if</code> statement converts <code>5 0/16</code> into <code>5</code> and <code>5 16/16</code> into <code>6</code>.<br> If you prefer the <code>5 0/16</code> and <code>5 16/16</code> notation, replace the <code>if</code> statement by:</p> <pre><code>return [NSString stringWithFormat:@"%d %d/16",(int)input,fractions]; </code></pre> <p><strong>EDIT:</strong> <em>(by Jason)</em></p> <pre><code>//Just to make it a little sweeter! - (NSString *)theFunction:(float)input { int fractions = lroundf((input - (int)input)/((float)1/(float)16)); if(fractions == 0 || fractions == 16) { return [NSString stringWithFormat:@"%d",lroundf(input)]; } else if(fractions == 2) { return [NSString stringWithFormat:@"%d 1/8",(int)input]; } else if(fractions == 4) { return [NSString stringWithFormat:@"%d 1/4",(int)input]; } else if(fractions == 6) { return [NSString stringWithFormat:@"%d 3/8",(int)input]; } else if(fractions == 8) { return [NSString stringWithFormat:@"%d 1/2",(int)input]; } else if(fractions == 10) { return [NSString stringWithFormat:@"%d 5/8",(int)input]; } else if(fractions == 12) { return [NSString stringWithFormat:@"%d 3/4",(int)input]; } else if(fractions == 14) { return [NSString stringWithFormat:@"%d 7/8",(int)input]; } else { return [NSString stringWithFormat:@"%d %d/16",(int)input,fractions]; } } </code></pre> <p><strong>EDIT</strong> (Response to edit by Jason)</p> <p>I optimized your code, this way it's much cleaner.<br> Also check the code below, I think it's more efficient to use an array.</p> <pre><code>- (NSString *)theFunction:(float)input { NSArray * array = [[NSArray alloc] initWithObjects: @"",@"",@"1/8",@"",@"1/4",@"",@"3/8",@"",@"1/2",@"",@"5/8",@"",@"3/4",@"",@"3/4",@"",@"7/8",@"",nil]; int fractions = lroundf((input - (int)input)/((float)1/(float)16)); if(fractions == 0 || fractions == 16) { return [NSString stringWithFormat:@"%d",lroundf(input)]; } else { if([[array objectAtIndex:fractions] isEqualToString:@""]) { return [NSString stringWithFormat:@"%d %d/16",(int)input,fractions]; } else { return [NSString stringWithFormat:@"%d %@",(int)input,[array objectAtIndex:fractions]]; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload