【问题标题】:NSNumberFormatter display 1,000,000 as 1mln etcNSNumberFormatter 将 1,000,000 显示为 100 万等
【发布时间】:2011-05-05 20:57:05
【问题描述】:

如标题中所述,有没有办法将巨大的数字(如 1,000,000(1 百万)或 45,500,000(45,500 万))格式化为字符串以显示该数字名称的缩短版本。我只是想阻止所有建议手动进行。我知道如何做到这一点。我只是想知道使用 NSNumberFormatter 是否有更简单的方法。

干杯,

卢卡斯

【问题讨论】:

    标签: iphone ios formatting nsnumber nsnumberformatter


    【解决方案1】:

    我建议结合使用手动和使用 NSNumberFormatter。我的想法是继承 NSNumberFormatter。如果您要格式化的数字> 1,000,000,则可以将其除以,使用超级实现对结果进行格式化,并在末尾附加“mln”。只做你不能为你做的部分。

    【讨论】:

      【解决方案2】:

      这是一个 NSNumberFormatter 子类的粗略草图(抱歉,格式有点不对):

      @implementation LTNumberFormatter
      
      @synthesize abbreviationForThousands;
      @synthesize abbreviationForMillions;
      @synthesize abbreviationForBillions;
      
      -(NSString*)stringFromNumber:(NSNumber*)number
      {
      if ( ! ( abbreviationForThousands || abbreviationForMillions || abbreviationForBillions ) )
      {
          return [super stringFromNumber:number];
      }
      
      double d = [number doubleValue];
      if ( abbreviationForBillions && d > 1000000000 )
      {
          return [NSString stringWithFormat:@"%@ %@", [super stringFromNumber:[NSNumber numberWithDouble:d / 1000000000]], abbreviationForBillions];
      }
      if ( abbreviationForMillions && d > 1000000 )
      {
          return [NSString stringWithFormat:@"%@ %@", [super stringFromNumber:[NSNumber numberWithDouble:d / 1000000]], abbreviationForMillions];
      }
      if ( abbreviationForThousands && d > 1000 )
      {
          return [NSString stringWithFormat:@"%@ %@", [super stringFromNumber:[NSNumber numberWithDouble:d / 1000]], abbreviationForThousands];
      }
          return [super stringFromNumber:number];
      }
      
      @end
      

      【讨论】:

        【解决方案3】:

        不,我不认为有办法用 NSNumberFormatter 做到这一点。在这件事上你是靠自己的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-06
          • 2019-10-11
          • 2011-08-29
          • 1970-01-01
          • 1970-01-01
          • 2017-12-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多