iPhone:如何从Objective-C中的货币符号字符串中获取数字

时间:2012-12-22 03:03:14

标签: ios formatting nsnumberformatter

我试图从带有货币符号的字符串中获取数字(浮点值)。

例如。  从“¥1,234”到1234  从“AU $ 3,456”到3456  从“56.78€”到56.78

我尝试了以下代码,但结果是0。

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLenient:YES];
NSNumber *number = [formatter numberFromString:text];
NSDecimalNumber *money = [NSDecimalNumber decimalNumberWithDecimal:[number decimalValue]];
float fval = [money floatValue];

提前致谢!

3 个答案:

答案 0 :(得分:8)


这里的困难在于货币小数分隔符对于不同的国家是不同的。更糟糕的是,某些区域中使用的数字分组符号在其他区域中用作小数分隔符。见:

http://en.wikipedia.org/wiki/Decimal_mark#Hindu.E2.80.93Arabic_numeral_system

http://en.wikipedia.org/wiki/Decimalisation#Decimal_currency

因此,依赖小数分隔符符号的天真方法,或者使用可疑小数标记后的小数位数可能不是100%准确。如果您正在处理某个特定的子集,请务必使用某种启发式方法。

考虑到全局问题空间,我认为最好的办法是使用语言环境,如果你有的话:

NSString* text =  @"$3,456.78 USD";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
text = [text stringByReplacingOccurrencesOfString:formatter.internationalCurrencySymbol withString:@""];
float fval = [formatter numberFromString:text].floatValue;

答案 1 :(得分:2)

我确信有更有效的方法,但你可以:

NSArray *strings = @[@"¥1,234",@"AU$3,456.78",@"56.78€"];

NSCharacterSet *nonNumbersSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];

for (NSString *string in strings)
{
    NSString *result = [string stringByTrimmingCharactersInSet:nonNumbersSet]; 
    result = [result stringByReplacingOccurrencesOfString:@"," withString:@""]; 
    CGFloat floatResult = [result floatValue];
    NSLog(@"%@ -> %.2f", result, floatResult);
}

或者,您可以使用扫描仪:

NSArray *strings = @[@"¥1,234",@"AU$3,456.78",@"56.78€"];
NSCharacterSet *numbersSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789.,"];

for (NSString *string in strings)
{
    NSString *result;
    NSScanner *scanner = [NSScanner scannerWithString:string];
    [scanner scanUpToCharactersFromSet:numbersSet intoString:nil];
    [scanner scanCharactersFromSet:numbersSet intoString:&result];
    result = [result stringByReplacingOccurrencesOfString:@"," withString:@""];
    CGFloat floatResult = [result floatValue];
    NSLog(@"%@ -> %.2f", result, floatResult);
}

当然,这些假设数字使用逗号作为千位分隔符。

您还可以使用NSNumberFormatter

NSArray *strings = @[@"¥1,234",@"AU$3,456.78",@"56.78€"];
NSCharacterSet *nonNumbersSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789.,"] invertedSet];

for (NSString *string in strings)
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    NSString *result = [string stringByTrimmingCharactersInSet:nonNumbersSet];
    NSNumber *numberResult = [formatter numberFromString:result];
    NSLog(@"%@ -> %.2f", result, [numberResult floatValue]);
}

答案 2 :(得分:0)

在Swift 4中

extension String
{
    var asDoubleValue:Double {
        let formatter = NumberFormatter()
        formatter.numberStyle = .currency
        formatter.locale = Locale(identifier: "en_IN")
        let noSymbolText = self.replacingOccurrences(of: formatter.internationalCurrencySymbol, with: "")
        guard let DoubleNumber=formatter.number(from: noSymbolText) as? Double else {
            return 0
        }

        return DoubleNumber
    }
}