gpt4 book ai didi

ios - "isnan"似乎不起作用

转载 作者:行者123 更新时间:2023-11-29 04:00:32 25 4
gpt4 key购买 nike

只是 try catch 非数字输入阅读很多内容。尝试了decimalDigitCharacterSet(发现很难相信以“decimal”一词开头的东西不包含小数)。尝试可变字符集来添加小数。一直在努力将“10.5”与“96”包含在一起,但仍然排除“abc”。

无论我在textbox1中输入什么内容,以下代码都会生成“IS a number”

double whatTheHey;

whatTheHey = _textBox1.text.doubleValue;

if isnan(whatTheHey) {
_textBox2.text = @"NOT a number > ";
}

if (!isnan(whatTheHey)) {
_textBox2.text = @"IS a number > ";
}

10.5、99、qwerty 都产生“IS a number”

仅仅为了捕获非数字输入,这似乎需要大量工作。

有人有任何明显简单的工作代码示例来捕获非数字但接受带小数的数字吗?

最佳答案

NaN not 字面意思是“任何不是数字的东西”。它是特定值的名称,即在某些不确定操作(例如零除以零或取负数的平方根)之后可以有一个浮点。请参阅the Wikipedia entry了解更多历史。

要实际解析字符串的数值,您可能需要查看 NSNumberFormatter :

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *a = [numberFormatter numberFromString:@"10.5"];
NSNumber *b = [numberFormatter numberFromString:@"96"];
NSNumber *c = [numberFormatter numberFromString:@"abc"];
NSLog(@"a: %@, b: %@, c: %@", a, b, c);

产量:a:10.5,b:96,c:(空)

满足您的特定标准的更简单(如果不太灵活)的解决方案可能是:

BOOL isNumber(NSString *aString){
return [aString integerValue] || [aString floatValue];
}

但是,如果您正在为 iOS(或 OS X)编写代码,那么您确实应该熟悉 NSFormatter。它们会让您的生活变得更加轻松。

关于ios - "isnan"似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15904322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com