如何在不使用 numberfromstring 的情况下将 NSString 转换为 NSNumber

发布于 2024-09-07 18:33:21 字数 1051 浏览 4 评论 0原文

我想将 NSString 转换为 NSNumber 而不使用 numbreFromString: 方法? numbreFromString: metgod 在 4.0 中表现得很奇怪。所以我想使用另一种方法将 NSString 转换为 NSNumber。请帮助我....

+ (NSString *)formatText:(NSString *) text withLocalSettings:(BOOL) isLacale {  

    NSNumber *aNsNumber= [numberFormatter numberFromString: text];  
    NSString *returnString = [NSString stringWithString:[numberFormatter stringForObjectValue:aNsNumber]];

    if(isLacale) {
        return returnString;
    }
    else {
        return [NSString stringWithFormat:@"%lf",[aNsNumber doubleValue]];
    }
} 

0

我正在开发一个应用程序,我需要在 3.0 和 4.0 中运行我的应用程序。我有一个文本字段,当我尝试在文本字段中输入数字时,行为是这样的...在 3.0 中:- 它允许输入 7 位数字和 2 个小数值(我已将其格式化为这样)。我根据所选国家/地区对数字以及逗号分隔进行了格式化和本地化。它在 3.0 和 3.1.2 IN 4.0 中完美运行

: - 它允许您仅输入 4 个数字,输入第 5 个数字后,它会使文本字段为空。当您输入第 5 个数字和输入第 6 个数字时,不会显示任何内容数字从第一个数字开始,一直到 4 个数字。例如: - 当您输入 1234 时,文本字段出现 - 1234,当您输入 12345 时,文本字段出现“ ”。当你现在输入 6 时,它会以 6 开头,依此类推。

我正在使用 NSNumberFormatter 和 numberfromstring 方法来格式化在文本字段中输入的值。

我无法理解为什么会这样......请帮助我......

I want to convert NSString to NSNumber without using numbreFromString: method? The numbreFromString: metgod is acting weirdly in 4.0. So i want to use an alternative to convert NSString to NSNumber. Please Help me....

+ (NSString *)formatText:(NSString *) text withLocalSettings:(BOOL) isLacale {  

    NSNumber *aNsNumber= [numberFormatter numberFromString: text];  
    NSString *returnString = [NSString stringWithString:[numberFormatter stringForObjectValue:aNsNumber]];

    if(isLacale) {
        return returnString;
    }
    else {
        return [NSString stringWithFormat:@"%lf",[aNsNumber doubleValue]];
    }
} 

0

I am developing an application i need to run my app both in 3.0 and 4.0. I have a textfield where when i try to enter numbers in the textfield the behaviour is like this... IN 3.0 :- It allows to enter 7 digits and 2 fractional values (I have formatted it like this). I have formatted and localized the numbers along with the comma seperations depending on the country selected. It is working perfectly in 3.0 and 3.1.2

IN 4.0 : - It allows you to enter only 4 numbers and after entering 5th digit it is making the textfields empty.. Nothing is displayed when u enter the 5th number and when u enter the 6th number it starts from the 1st number and continues the same til 4 numbers. ex: - when u enter 1234, textfield appears - 1234 and when u enter 12345, textfield appears " ". and when u enter 6 now it starts with 6 and so on..

I am using the NSNumberFormatter and numberfromstring method to format the values entered in the textfield.

I am not able to understand why this is happening like this... Please help me...

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

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

发布评论

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

评论(2

转角预定愛 2024-09-14 18:33:21
[NSNumber numberWithInteger:[theString integerValue]];
[NSNumber numberWithDouble:[theString doubleValue]];

还有 floatValue、intValue、longLongValue、boolValue

编辑:

要去掉逗号,请在执行上述操作之前使用 stringByReplacingOccurrencesOfString。

theString = [theString stringByReplacingOccurrencesOfString:@"," withString:@""];

要删除多个字符,您可以使用 ComponentsSeparatedByCharactersInSet 来获取一些内容。

theSet = [NSCharacterSet characterSetWith...];
theString = [[theString componentsSeparatedByCharactersInSet:theSet] componentsJoinedByString:@""];
[NSNumber numberWithInteger:[theString integerValue]];
[NSNumber numberWithDouble:[theString doubleValue]];

also floatValue, intValue, longLongValue, boolValue

Edit:

to strip out commas, use stringByReplacingOccurrencesOfString before doing the above.

theString = [theString stringByReplacingOccurrencesOfString:@"," withString:@""];

to strip out more than one character, you could get something working with componentsSeparatedByCharactersInSet.

theSet = [NSCharacterSet characterSetWith...];
theString = [[theString componentsSeparatedByCharactersInSet:theSet] componentsJoinedByString:@""];
橙味迷妹 2024-09-14 18:33:21

iOS4 在数字格式方面非常挑剔。

在您的示例代码中,您传递了一个区域设置 - 确保字符串中的小数分隔符与区域设置中定义的一致。确保您输入的数字格式正确 (iOS4),如果您使用 DecimalStyle,则 id est 九千应该是 9.000 而不是 9000。

如果您坚持使用替代解析器 - sscanf,atoi 可能是一个选择。

根据需要转换的数字,使用标准 c lib 的 sscanf 和格式化程序或 atoi。

#include<stdio.h>

int main(int argc, char **argv) {

   char s[] = "1.2";
   double x;

   sscanf(s, "%lf", &x);
   printf("The decimal number was %g\n\n", x);

   return 1;
}

atoi 接受一个包含整数的字符串并以 int 形式返回该值。

iOS4 is very picky when it comes to formatting of numbers.

In your example code you pass in a locale - make sure that the decimal separator in your string is in line with what is defined in the locale. Make sure that you enter numbers that are correctly formatted (iOS4) id est nine thousand should be 9.000 and not 9000 if your are using DecimalStyle.

If you insist on using an alternative parser - sscanf an atoi could be an option.

Use standard c lib's sscanf with formatter or atoi depending on the numbers you need to convert.

#include<stdio.h>

int main(int argc, char **argv) {

   char s[] = "1.2";
   double x;

   sscanf(s, "%lf", &x);
   printf("The decimal number was %g\n\n", x);

   return 1;
}

atoi takes a string containing an integer and returns the value as an int.

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