NSNumberFormatter 与 NSNumberFormatterPercentStyle 的怪异

发布于 2024-08-27 06:46:35 字数 590 浏览 3 评论 0原文

我需要解析 UITextField 中的一些文本并将其转换为百分比。理想情况下,我希望用户在文本字段中输入 12 或 12% 之类的内容,并将其解析为百分比数字。

这就是奇怪的地方。数字格式化程序似乎不喜欢 12,并且似乎将 12% 除以 10000 而不是 100:

NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];

NSNumber *n1 = [formatter numberFromString:@"12"];   
NSNumber *n2 = [formatter numberFromString:@"12%"];  

NSLog(@"n1 = %@", n1);  // n1 = (null)
NSLog(@"n2 = %@", n2);  // n2 = 0.0012

如何让格式化程序按预期返回 0.12?

编辑:这似乎只有在格式化程序首先失败时才会发生。如果格式化程序没有失败,它将按预期返回 0.12。奇怪的。

I need to parse some text in a UITextField and turn it into a percentage. Ideally, I'd like the user to either type something like 12 or 12% into the text field and have that be parsed into a number as a percentage.

Here's what's weird. The number formatter seems to not like 12 and seems to divide 12% by 10000 instead of 100:

NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];

NSNumber *n1 = [formatter numberFromString:@"12"];   
NSNumber *n2 = [formatter numberFromString:@"12%"];  

NSLog(@"n1 = %@", n1);  // n1 = (null)
NSLog(@"n2 = %@", n2);  // n2 = 0.0012

How do I get the formatter to return 0.12 as expected?

EDIT: it seems to only happen if the formatter fails first. If the formatter does not fail it returns 0.12 as expected. Strange.

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

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

发布评论

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

评论(2

蓝眼泪 2024-09-03 06:46:35

这是一个似乎对我有用的解决方法:

我对 NSNumberFormatter 进行子类化并实现了以下方法。似乎对我有用。

- (NSNumber *)numberFromString:(NSString *)string { 
    NSNumber *retValue = [super numberFromString:string];
    if ([self numberStyle] ==   NSNumberFormatterPercentStyle) {
        [super setNumberStyle:NSNumberFormatterPercentStyle];   
    }
    return (retValue);
}

- (NSString *)stringFromNumber:(NSNumber *)number {
    NSString * str = [super stringFromNumber:number];
    if ([self numberStyle] ==   NSNumberFormatterPercentStyle) {
        [super setNumberStyle:NSNumberFormatterPercentStyle];   
    }
    return (str);
}

Here's a workaround that seemed to work for me:

I subclassed NSNumberFormatter and implemented the following methods. Seemed to work for me.

- (NSNumber *)numberFromString:(NSString *)string { 
    NSNumber *retValue = [super numberFromString:string];
    if ([self numberStyle] ==   NSNumberFormatterPercentStyle) {
        [super setNumberStyle:NSNumberFormatterPercentStyle];   
    }
    return (retValue);
}

- (NSString *)stringFromNumber:(NSNumber *)number {
    NSString * str = [super stringFromNumber:number];
    if ([self numberStyle] ==   NSNumberFormatterPercentStyle) {
        [super setNumberStyle:NSNumberFormatterPercentStyle];   
    }
    return (str);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文