我需要将货币从字符串格式转换为数字格式。
例如,我的字符串变量值为@"10 lakh"
。转换为数字格式的字符串值为1000000。如何进行这种转换?
(将100万转换为1000000,这就是问题所在)
最佳答案
尝试
- (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);
}
}
关于iphone - 如何将货币从字符串格式转换为数字格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16914458/