iphone - 如何在 NSNumber 上做数学运算

标签 iphone ios objective-c cocoa nsnumber

我正在尝试获取传感器的读数并将其显示为百分比。 [某些值] 将始终介于 0 和 1 之间。

这是我的代码:

NSNumber *reading = [some value];
reading = [reading floatValue] * 100;

但是,我得到“Assigning to NSNumber *_strong from incompatible type float”

我是 NSNumber 对象的新手,并且很难理解如何将我的传感器读数显示为用户的百分比。例如:75%

感谢您的帮助

最佳答案

您需要将整数或浮点值装箱以将其存储在 NSNumber 中,

作为:

NSNumber *reading = @(10.123);
reading = @([reading floatValue] * 100);

在此之后,您可以打印/将其转换为字符串:

NSString *display=[NSString stringWithFormat:@"%@%%",reading];

注意 %% 双百分号

关于iphone - 如何在 NSNumber 上做数学运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15437913/

相关文章:

iphone - 使用官方 iOS SDK 检测 SIM 卡更改或查找 SIM 卡的 IMSI?

iphone - 隐藏 TabBarController 中的项目 [xcode6/swift]

iphone - iOS UIWebView 大小基于窗口

ios - 使用 CocoaPods 的多个应用程序的项目/工作区结构

iOS模拟器: Which folders can be safely deleted?

ios - viewDidLoad 和 viewDidAppear 之间的延迟

iphone - 如何在编辑时更改 returnKeyType?

ios - Google Analytics iOS SDK,调度间隔很长

iphone - UITableViewCell 文本标签偏移量

javascript - 如何在 iPad 中调整/重新缩放网页