2 developerwab developerWab 于 2013.06.05 11:24 提问

如例子中转换格式到数字格式

转换货币格式到数字格式

比如字符串变量是@"10 lakh"转换成数字值后应该是1000000。应该怎么实现?

谢谢。

1个回答

gaoXxxing
gaoXxxing   2013.06.05 17:27
已采纳
- (NSNumber *)multiplierForKey:(NSString *)key
{

    NSDictionary *dict = @{@"thousand":@1000, @"lakh":@100000, @"million":@1000000, @"crore":@100000000};
    NSNumber *value = dict[[key lowercaseString]];
    return value?value:@1;
}

- (void)findMultiplier{

    NSString *string = @"10 lakh";

    NSArray *components = [string componentsSeparatedByString:@" "];

    if ([components count]==2) {
        NSNumber *value = components[0];
        NSString *key = components[1];
        NSNumber *multiplier = [self multiplierForKey:key];

        NSDecimalNumber *decimalValue = [NSDecimalNumber decimalNumberWithDecimal:[value decimalValue]];
        NSDecimalNumber *multiplierValue = [NSDecimalNumber decimalNumberWithDecimal:[multiplier decimalValue]];

        NSDecimalNumber *finalValue = [decimalValue decimalNumberByMultiplyingBy:multiplierValue];
        NSLog(@"Value : %@",finalValue);
    }
}
Csdn user default icon
上传中...
上传图片
插入图片