我在分隔9位数的数字时遇到问题。我这样做是使用以下方法:
注意:将thuis粘贴到Xcode中可能更容易“更容易”看到它:
int firstDigit = currentValue *.00000001; // = 3.7 = 3
firstDigitLabel.text = [NSString stringWithFormat:@"%d",firstDigit];
int secondDigit = (currentValue *.0000001) - (firstDigit *10); // = 7.2 = 7
secondDigitLabel.text = [NSString stringWithFormat:@"%d",secondDigit];
int thirdDigit = (currentValue *.000001) - ((firstDigit *100)+(secondDigit*10)); // = 2.8 = 2
thirdDigitLabel.text = [NSString stringWithFormat:@"%d",thirdDigit];
int fourthDigit = (currentValue *.00001) - ((firstDigit *1000)+(secondDigit*100)+(thirdDigit*10)); // = 2.8 = 2
fourthDigitLabel.text = [NSString stringWithFormat:@"%d",fourthDigit];
int fifthDigit = (currentValue *.0001) - ((firstDigit *10000)+(secondDigit*1000)+(thirdDigit*100)+(fourthDigit*10));
fifthDigitLabel.text = [NSString stringWithFormat:@"%d",fifthDigit];
int sixthDigit = (currentValue *.001) - ((firstDigit *100000)+(secondDigit*10000)+(thirdDigit*1000)+(fourthDigit*100)+(fifthDigit*10));
sixthDigitLabel.text = [NSString stringWithFormat:@"%d",sixthDigit];
int seventhDigit = (currentValue *.01) - ((firstDigit *1000000)+(secondDigit*100000)+(thirdDigit*10000)+(fourthDigit*1000)+(fifthDigit*100)+(sixthDigit*10));
seventhDigitLabel.text = [NSString stringWithFormat:@"%d",seventhDigit];
int eighthDigit = (currentValue *.1) - ((firstDigit *10000000)+(secondDigit*1000000)+(thirdDigit*100000)+(fourthDigit*10000)+(fifthDigit*1000)+(sixthDigit*100)+(seventhDigit*10));
eighthDigitLabel.text = [NSString stringWithFormat:@"%d",eighthDigit];
int ninthDigit = (currentValue *1) - ((firstDigit *100000000)+(secondDigit*10000000)+(thirdDigit*1000000)+(fourthDigit*100000)+(fifthDigit*10000)+(sixthDigit*1000)+(seventhDigit*100)+(eighthDigit*10)+1);
ninthDigitLabel.text = [NSString stringWithFormat:@"%d",ninthDigit];
虽然这可以找到前7个数字,但我遇到了找到第8个数字的问题,因此找到了第9个数字(它们相互构建)。这很奇怪,因为当我自己使用第8和第9位方程式进行数学计算时,它们可以工作,只有当我将它们添加到应用程序时才会开始搞乱。
答案 0 :(得分:2)
我相信这是一种更有效(和可读)的方式:
int currentValue = someNineDigitNumber;
NSString *currentValueString = [NSString stringWithFormat: @"%i",currentValue];
NSString *firstDigit = [currentValueString substringWithRange: NSMakeRange(0,1)];
NSString *secondDigit = [currentValueString substringWithRange: NSMakeRange(1,1)];
NSString *thirdDigit = [currentValueString substringWithRange: NSMakeRange(2,1)];
NSString *fourthDigit = [currentValueString substringWithRange: NSMakeRange(3,1)];
NSString *fifthDigit = [currentValueString substringWithRange: NSMakeRange(4,1)];
NSString *sixthDigit = [currentValueString substringWithRange: NSMakeRange(5,1)];
NSString *seventhDigit = [currentValueString substringWithRange: NSMakeRange(6,1)];
NSString *eighthDigit = [currentValueString substringWithRange: NSMakeRange(7,1)];
NSString *ninthDigit = [currentValueString substringWithRange: NSMakeRange(8,1)];
firstDigitLabel.text = firstDigit;
secondDigitLabel.text = secondDigit;
thirdDigitLabel.text = thirdDigit;
fourthDigitLabel.text = fourthDigit;
fifthDigitLabel.text = fifthDigit;
sixthDigitLabel.text = sixthDigit;
seventhDigitLabel.text = seventhDigit;
eighthDigitLabel.text = eighthDigit;
ninthDigitLabel.text = ninthDigit;
如果您对代码有任何疑问,请在评论中提问。希望这有帮助!
答案 1 :(得分:2)
您似乎不知道除法(/
)和模(%
)运算符。您可以使用模运算符(%
)来获取值的单位数字,然后使用除法运算符(/
)删除该数字并将所有其他数字向下移位。
int digits[9];
int value = currentValue;
BOOL isNegative = value < 0;
if (isNegative) {
value = -value;
}
for (int i = 0; i < 9; ++i) {
digits[8 - i] = value % 10;
value /= 10;
}