将 NSMutableString 转换为浮点值

发布于 2024-12-17 08:43:54 字数 1794 浏览 5 评论 0原文

在我的应用程序中,我从一堆滑块获取值并将它们转换为 AppDelegate 中的 NSMutableString。然而,我需要稍后回忆这些值,以便我可以用它们进行一些计算,但我无法将这些值重新转换回浮点数,我每次都只是得到 0。我该怎么做才能将 NSMutableString 转换为浮点值?

这是我的视图控制器代码,它尝试从委托获取 NSMutable 字符串,转换它们并进行计算。

-(IBAction)sliderValue:(id)sender {

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];



float x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,compat;



x1 = [appDelegate.x1 floatValue];
x2 = [appDelegate.x2 floatValue];
x3 = [appDelegate.x3 floatValue];
x4 = [appDelegate.x4 floatValue];
x5 = [appDelegate.x5 floatValue];
x6 = [appDelegate.x6 floatValue];
x7 = [appDelegate.x7 floatValue];
x8 = [appDelegate.x8 floatValue];
x9 = [appDelegate.x9 floatValue];
x10 = [appDelegate.x10 floatValue];
x11 = [appDelegate.x11 floatValue];
x12 = [appDelegate.x12 floatValue];
x13 = [appDelegate.x13 floatValue];
x14 = [appDelegate.x14 floatValue];
x15 = [appDelegate.x15 floatValue];
x16 = [appDelegate.x16 floatValue];
x17 = [appDelegate.x17 floatValue];
x18 = [appDelegate.x18 floatValue];
x19 = [appDelegate.x19 floatValue];
x20 = [appDelegate.x20 floatValue];

compat = (40-(fabsf(x1-x11)+fabsf(x3-x13)+fabsf(x6-x16)+fabsf(x8-x18))+(x2+x5+x7+x10+x12+x15+x17+x20)*(1-fabsf(x10-x20)/10)/2+(2-fabsf(x4-x14)/5)*(x5+x15)/2);


NSMutableString *strAnswer = [[NSMutableString alloc] initWithString:@"Your Compatibility is "];
NSString *score = [[NSString alloc] initWithFormat:@"%.2f",x2];


[strAnswer appendString:score];
[strAnswer appendString:@" % "];
result.text = appDelegate.x2;

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The Results Are IN!!" message:strAnswer delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alert show];

我到底

如何实现这里的 NSScanner 方法?

In my app I am getting values from a bunch of sliders and converting them to a NSMutableString in the AppDelegate. However, I need to recall these values later on so I can do some calculations with them, but I can't reconvert these values back to a float, I just get 0 every time. What can I do to convert the NSMutableString to a float value?

Here is my code for the view controller that is trying to get the NSMutable strings from the delegate, convert them, and do a calculation.

-(IBAction)sliderValue:(id)sender {

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];



float x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,compat;



x1 = [appDelegate.x1 floatValue];
x2 = [appDelegate.x2 floatValue];
x3 = [appDelegate.x3 floatValue];
x4 = [appDelegate.x4 floatValue];
x5 = [appDelegate.x5 floatValue];
x6 = [appDelegate.x6 floatValue];
x7 = [appDelegate.x7 floatValue];
x8 = [appDelegate.x8 floatValue];
x9 = [appDelegate.x9 floatValue];
x10 = [appDelegate.x10 floatValue];
x11 = [appDelegate.x11 floatValue];
x12 = [appDelegate.x12 floatValue];
x13 = [appDelegate.x13 floatValue];
x14 = [appDelegate.x14 floatValue];
x15 = [appDelegate.x15 floatValue];
x16 = [appDelegate.x16 floatValue];
x17 = [appDelegate.x17 floatValue];
x18 = [appDelegate.x18 floatValue];
x19 = [appDelegate.x19 floatValue];
x20 = [appDelegate.x20 floatValue];

compat = (40-(fabsf(x1-x11)+fabsf(x3-x13)+fabsf(x6-x16)+fabsf(x8-x18))+(x2+x5+x7+x10+x12+x15+x17+x20)*(1-fabsf(x10-x20)/10)/2+(2-fabsf(x4-x14)/5)*(x5+x15)/2);


NSMutableString *strAnswer = [[NSMutableString alloc] initWithString:@"Your Compatibility is "];
NSString *score = [[NSString alloc] initWithFormat:@"%.2f",x2];


[strAnswer appendString:score];
[strAnswer appendString:@" % "];
result.text = appDelegate.x2;

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The Results Are IN!!" message:strAnswer delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alert show];

}

How exactly would I implement the NSScanner method is here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

月依秋水 2024-12-24 08:43:54

使用 floatValue NSString 方法或 NSScanner,更强大、更灵活。我个人更喜欢后者,因为如果接收器不以浮点数的有效文本表示形式开头,则 floatValue 返回 0.0。因此您无法区分 @"0.0"@"completegarbage"

Use the floatValue NSString method or a NSScanner, which is more powerful and flexible. I personally prefer the latter as floatValue returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number. So you can't distinguish between @"0.0" and @"completegarbage".

盛装女皇 2024-12-24 08:43:54

像这样的东西

NSMutableString *str = [[NSMutableString alloc] init];
[str setString:@"123.4"];

float f = [str floatValue];

Something like this

NSMutableString *str = [[NSMutableString alloc] init];
[str setString:@"123.4"];

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