iphone - 隔离大数的数字(iOS-Xcode)

标签 iphone ios

我在分隔 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;

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

关于iphone - 隔离大数的数字(iOS-Xcode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11218302/

相关文章:

php - 未从 iPhone 接收到 POST 数据

ios - bringSubviewToFront 不适用于下拉 View (UIView)

iphone - 如何将数字画成圆形?

iphone - iOS 中的 Asyncsocket 断开连接事件

android - StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

iphone - 关于 UITableViewController Storyboard的建议

ios - 如何在 UIPageViewController 的导航栏上显示按钮?

ios - 为什么我的 UITableView 的顶部有额外的填充,在 iOS7 中具有 UITableViewStyleGrouped 样式

ios - Wintermute iOS 应用获取 "EXC_BAD_ACCESS"

iphone - uibutton 的触摸事件结束