objective-c - 从 int/float 中获取前三位数字

标签 objective-c iphone

我想从大 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/

相关文章:

ios - 在 iOS 设备上本地保存解析对象

iphone - NSSearchPathForDirectoriesInDomains 麻烦(掉头发快)

objective-c - Google 云存储 objective-c 示例

ios - 使用 xcode11 在 ios 13 上运行项目时,基于 View Controller 的旋转停止工作

iphone - 自定义部分名称导致 NSFetchedResultsController 崩溃

iphone - 用于视频录制的 iOS 数字变焦。如何高效实现?

iphone - UIWebView 不会加载带有证书的链接 (https ://prefix)

iphone - 在 UIScrollview 中拖动会增加其 subview 数

java - 通过AsyncSocket向objective-c中的java服务器发送消息

由 iBeacon 触发的 iOS 8 后台位置更新