7

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

例如。从 "¥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];

提前致谢!

4

3 回答 3

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;
于 2012-12-22T04:41:35.337 回答
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]);
}
于 2012-12-22T03:56:48.440 回答
0

在斯威夫特 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
    }
}
于 2017-12-19T09:40:01.790 回答