【问题标题】:Currency Formatter for different Locale in iOSiOS中不同语言环境的货币格式化程序
【发布时间】:2013-07-04 06:30:39
【问题描述】:

我想将此字符串: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];

【问题讨论】:

  • 输入的字符串总是7位吗?
  • 您需要为此编写自己的格式化程序
  • 我很好奇你为什么需要这样的逗号?
  • @wain 在印度,他们有 100,000(十万)的词,所以 10,00,000 是 100 万。因此,格式在该语言环境中是有意义的。
  • @Wain:这是一种印度货币格式。所以,OP 必须将@"en_IN" 设置为currencyFormatter Locale。检查我的答案。

标签: iphone ios objective-c nsstring


【解决方案1】:

这是一种印度货币格式。因此,您必须将@"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]]);

【讨论】:

    【解决方案2】:

    对于 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)
    

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 2011-10-07
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多