0

我在分隔 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 位方程式进行数学运算时,它们起作用了,而且只有当我将它们添加到应用程序时,它们才开始搞砸。

4

2 回答 2

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;
}
于 2012-06-27T03:34:06.627 回答
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;

如果您对代码有任何疑问,请在评论中提问。希望这可以帮助!

于 2012-06-27T02:04:25.377 回答