是的,我已经看到了这个问题,但它是针对 php 的。我还是编程新手,很惊讶没有为此发布 iOS 问题。这就是我将小数转换为分数的方式。认为它可能有助于从英制到公制的格式。所以问题是我将如何在 iOS 中从英制转换为公制?
//Converts Decimals to Fractions.
- (NSString *)DecToFrac:(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]];
}
}
}