Possible Duplicates:
How to convert floats to human-readable fractions?
Convert decimal to fraction in Objective-C?
I want to take a decimal, 5.50
, which is in a variable, and divide only the fractional part by 0.0625
, which is my accuracy point. This would give me 8
, as in 8/16
or 1/2
.
Then I would like to display that answer as 5 8/16
or 1/2
in a text field. I know some answers will return a decimal still and not a whole number when dividing by .0625
, but I would round that answer, which would still give me to the nearest 16th. What would be the best way to do this? I would like to make a function so I can reuse it. Yes, I posted a similar question that was answered, but wasn't able to get it to work. I'm thinking there's a better and easier way to do this, so I've posted this. Thanks in advance.
Function:
- (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];
}
}
Note:
The if
statement converts 5 0/16
into 5
and 5 16/16
into 6
.
If you prefer the 5 0/16
and 5 16/16
notation, replace the if
statement by:
return [NSString stringWithFormat:@"%d %d/16",(int)input,fractions];
EDIT: (by Jason)
//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];
}
}
EDIT (Response to edit by Jason)
I optimized your code, this way it's much cleaner.
Also check the code below, I think it's more efficient to use an array.
- (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]];
}
}
}