Iphone 隔离大数字(iOS Xcode)

Iphone 隔离大数字(iOS Xcode),iphone,ios,Iphone,Ios,我在9位数字中的数字分隔方面有问题。我使用以下方法进行此操作: 注意:将thuis粘贴到Xcode中可能更容易“查看”它: int firstDigit = currentValue *.00000001; // = 3.7 = 3 firstDigitLabel.text = [NSString stringWithFormat:@"%d",firstDigit]; int secondDigit = (currentValue *.0000001) - (firstDigit *10); /

我在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位的方程式计算时,它们是有效的,只有当我把它们添加到应用程序中时,它们才会开始出错

我认为这是一种更有效(更具可读性)的方法:

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;

如果您对代码有任何疑问,请在注释中提问。希望这有帮助

我认为这是一种更有效(更具可读性)的方法:

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;

如果您对代码有任何疑问,请在注释中提问。希望这有帮助

您似乎不知道除法(
/
)和模(
%
)运算符。您可以使用模运算符(
%
)获取值的单位数字,然后使用除法运算符(
/
)删除该数字并将所有其他数字下移

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;
}
int位数[9];
int值=当前值;
BOOL为负值=值<0;
如果(为负){
值=-值;
}
对于(int i=0;i<9;++i){
数字[8-i]=值%10;
数值/=10;
}

您似乎不知道除法(
/
)和模(
%
)运算符。您可以使用模运算符(
%
)获取值的单位数字,然后使用除法运算符(
/
)删除该数字并将所有其他数字下移

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;
}
int位数[9];
int值=当前值;
BOOL为负值=值<0;
如果(为负){
值=-值;
}
对于(int i=0;i<9;++i){
数字[8-i]=值%10;
数值/=10;
}

目前我无法测试,但我会尝试一下。谢谢你的帮助,我稍后会回来告诉你它是否有效。当然。慢慢来。请注意:在所有的
NSMakeRange
函数中,您可能需要将第一个参数向前移动一个值。例如,
NSMakeRange(0,1)
变为
NSMakeRange(1,1)
NSMakeRange(1,1)
变为
NSMakeRange(2,1)
,等等。我现在无法测试,但我会尝试一下。谢谢你的帮助,我稍后会回来告诉你它是否有效。当然。慢慢来。请注意:在所有的
NSMakeRange
函数中,您可能需要将第一个参数向前移动一个值。例如,
NSMakeRange(0,1)
变为
NSMakeRange(1,1)
NSMakeRange(1,1)
变为
NSMakeRange(2,1)
,等等!这将是我的答案,所以对你来说+1:)搞定了!这将是我的答案,所以对你来说+1:)