gpt4 book ai didi

iphone - 将选择器上的分数格式化为最小公分母?

转载 作者:行者123 更新时间:2023-11-28 19:19:14 26 4
gpt4 key购买 nike

下面是我目前如何格式化我的选择器。我真的很希望它显示 1/8 而不是 2/16 或 1/2 而不是 8/16。我该如何调整它以显示我想要的输出?谢谢!

fractionArray = [[NSMutableArray alloc] init];
for(int frac = 0; frac <= 15; frac ++){
NSString *fracString = [NSString stringWithFormat:@"%d/16", frac];
[fractionArray addObject:fracString]; // Add the string.

最佳答案

现在的 child ... Euclidean algorithm ...他们在学校教什么...咕咕咕...

int gcd(int a, int b) {
// assumes a >= 0 && b > 0
while (b != 0) {
int t = a % b;
a = b;
b = t;
}
return a;
}

NSString *stringByReducingFraction(int a, int b) {
if (a == 0) return @"0";
if (a == b) return @"1";

int g = gcd(a, b);
return [NSString stringWithFormat:@"%d/%d", a / g, b / g];
}

关于iphone - 将选择器上的分数格式化为最小公分母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10291274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com