4

我想将此字符串:转换1000000为以下格式:10,00,000但是,我得到这样的输出:1,000,000这是错误的。我需要10,00,000

我的代码:

NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
         inputnumber = newString;
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencySymbol:@""];
[formatter setMaximumFractionDigits:2];
[formatter setMinimumFractionDigits:2];
[formatter setUsesGroupingSeparator:YES];
[formatter setCurrencyGroupingSeparator:@","];
// [formatter setCurrencyDecimalSeparator:@"."];
[formatter setGroupingSeparator:@","];
NSNumber *num = [NSNumber numberWithDouble:[inputnumber doubleValue]];
inputnumber = [formatter stringFromNumber:num];
4

2 回答 2

7

这是一种印度货币格式。所以,你必须设置@"en_IN"currencyFormatter Locale.

工作代码:

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setCurrencySymbol:@""];
[currencyFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"] autorelease]];
NSLog(@"Final Value :: %@", [currencyFormatter stringFromNumber:[NSNumber numberWithFloat:1000000]]);
于 2013-07-04T06:41:23.613 回答
3

对于 Swift,请检查:

 let initalValue:NSString = "1000000"
    let currencyFormatter = NSNumberFormatter()
    currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    currencyFormatter.locale = NSLocale (localeIdentifier: "en_IN")
    let formattedValue = currencyFormatter.stringFromNumber(initalValue.doubleValue)
    print(formattedValue)
于 2016-08-19T07:26:16.523 回答