我想从大 float 或整数中获取前三位数字,并在某些情况下插入小数。例如:
KB
----------
32589 >> 325
43266 >> 432
MB
----------
1234599 >> 1.23
3422847 >> 3.42
对于特定的数字,我将拥有“KB”和“MB”字符串。这会让我知道是否需要小数,如“MB”示例中所示。我查看了 NSNumberFormatter 但不确定那里有什么帮助。有什么建议吗?
最佳答案
这里有一些快速而肮脏的代码,用于提取数字的前三位(NSString 格式):
long someNumber = 1234599;
NSString * allDigits = [NSString stringWithFormat:@"%l", someNumber];
NSString * topDigits = [allDigits substringToIndex:3];
NSLog(@"%@", topDigits); // will output 123
关于objective-c - 从 int/float 中获取前三位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/869093/