NSNumber 十进制到 C 原始值?

发布于 2024-12-12 05:21:28 字数 258 浏览 6 评论 0原文

我想将十进制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰葑 2024-12-19 05:21:28

由于看起来您正在处理小数部分,因此您希望将其转换为浮点型或双精度型以执行操作,具体取决于您期望该值有多大。除非你处理的是大得离谱或精确的数字,否则浮点数应该没问题。例如,它的外观如下:

float lValue = [[left valueForOutputKey:@"Y"] floatValue];

lValue *= 10;

if (lValue < 10) {
    // do whatever
}
else if (lValue > 50) {
    // do whatever
}

然后要将值存储回您的插座或其他任何位置,您将其打包回 NSNumber:

NSNumber *newValue = [NSNumber numberWithFloat:lValue];

[left setValue:newValue forKey:@"Y"];

您可能必须将 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:

float lValue = [[left valueForOutputKey:@"Y"] floatValue];

lValue *= 10;

if (lValue < 10) {
    // do whatever
}
else if (lValue > 50) {
    // do whatever
}

Then to store the value back in your outlet or whatever, you pack it back into a NSNumber:

NSNumber *newValue = [NSNumber numberWithFloat:lValue];

[left setValue:newValue forKey:@"Y"];

You may have to convert newValue into a string to display it in a control, just use [newValue stringValue] to do that.

喜你已久 2024-12-19 05:21:28

使用 NSNumber 的方法之一:

int leftInt = [Left intValue];
float leftFloat = [Left floatValue];
double leftDouble = [Left doubleValue];

Use one of the methods of NSNumber:

int leftInt = [Left intValue];
float leftFloat = [Left floatValue];
double leftDouble = [Left doubleValue];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文