Finding the numerator of a float's fractional value, given a specific denominator [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-11 08:26:07

问题


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.


回答1:


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]];
        }
    }

}



回答2:


I think your problem is much simpler than the one @Caleb linked to.

Divide by 0.0625 to get the number of sixteenths. Round the result to the nearest integer i. Use integer division i / 16 to get the number of whole units and use the modulo operator j = i % 16 to get the fractional units. To reduce the fractions, use j as an index into an array you create ahead of time as { "", "1/16", "1/8", "3/16", ... }.

You can also divide by 16 and get the modulo 16 using bitwise operators, but that will make the code harder to read for not much benefit.



来源:https://stackoverflow.com/questions/6061886/finding-the-numerator-of-a-floats-fractional-value-given-a-specific-denominato

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!