NSNumber 十进制到 C 原始值?
我想将十进制 NSNumber
转换为 int
或其他可以进行数学运算的形式。这是我的项目的带注释的代码:
NSNumber *Left = [left valueForOutputKey:@"Y"];
此行获取 Quartz Composer Outlet,通常其值约为 0.512。
基本上,我想将其乘以 10,然后执行一些大于和小于等操作来查看它在哪个范围内。
I want to convert a decimal NSNumber
to an int
or other form which I can do math with. Here's the annotated code for my project:
NSNumber *Left = [left valueForOutputKey:@"Y"];
This line gets a Quartz Composer Outlet, usually with a value around 0.512.
Basically, I want to multiply this by 10, and then do some operations like greater than and less than to see which range it is in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于看起来您正在处理小数部分,因此您希望将其转换为浮点型或双精度型以执行操作,具体取决于您期望该值有多大。除非你处理的是大得离谱或精确的数字,否则浮点数应该没问题。例如,它的外观如下:
然后要将值存储回您的插座或其他任何位置,您将其打包回 NSNumber:
您可能必须将 newValue 转换为字符串才能将其显示在控件中,只需使用 [newValue stringValue ] 这样做。
Since it looks like you're dealing with a fractional component, you want to convert it to a float or a double to perform your operations, depending on how big you expect that value to be. A float should be fine unless you're dealing with ridiculously large or precise numbers. Here's how it would look, for example:
Then to store the value back in your outlet or whatever, you pack it back into a NSNumber:
You may have to convert newValue into a string to display it in a control, just use [newValue stringValue] to do that.
使用
NSNumber
的方法之一:Use one of the methods of
NSNumber
: