【问题标题】:How to format long numbers?如何格式化长数字?
【发布时间】:2011-04-10 00:47:58
【问题描述】:

如果我有一个 100,000,000 的数字,如何在字符串中将其表示为“100M”?

【问题讨论】:

标签: java numbers number-formatting


【解决方案1】:

这是更通用的解决方案。

public static String abbreviateNumber(long num) {

    long temp = num / 1000000; 
    if(temp > 0) {
        return temp + "M+";
    }

    temp = num / 1000;
    if (temp > 0) {
        return temp + "K+";
    }

    temp = num / 500;
    if (temp > 0) {
        return  "500+";
    }

    temp = num / 100;
    if (temp > 0) {
        return  "100+";
    }

    temp = num / 50;
    if (temp > 0) {
        return  "50+";
    }

    temp = num / 10;
    if (temp > 0) {
        return  "10+";
    }

    return String.valueOf(num);
}

【讨论】:

    【解决方案2】:

    这是我的解决方案,使其更通用:

    private static final String[] magnitudes = new String[] {"", "K", "M"};
    
    public static String shortenNumber(final Integer num) {
        if (num == null || num == 0) 
            return "0";
    
        float res = num;
        int i = 0;
        for (; i < magnitudes.length; i++) {
            final float sm = res / 1000;
            if (sm < 1) break;
    
            res = sm;
        }
    
    
        // don't use fractions if we don't have to
        return ( (res % (int) res < 0.1) ?
                    String.format("%d", (int)res) :
                    String.format("%.1f", res)
                ) 
                + magnitudes[i];
    }
    

    【讨论】:

      【解决方案3】:

      有一种算法可以做到这一点:

      你需要一张看起来像这样的地图

      2 => "hundred"
      3 => "thousand"
      6 => "million"
      9 => "billion"
      12 => "trillion"
      15 => "quadrillion"
      

      ...等等...

      1) 取数字“num”,计算该数字的 log10 指数“ex”并取底。

      注意

      log10(0) 不存在,因此请检查 这个数字不是 0,因为它 输出一些东西没有意义 像 20 = "2 十" 你应该返回 如果它更小,它就是这个数字 超过 100 个!

      2) 现在遍历上面哈希映射的键并查看键是否匹配,如果不匹配,则取小于指数“ex”的键。

      3) 更新 "ex" 到这个键!

      4) 现在将数字格式化为

      num = num / pow(10, ex)

      (!! ex 是哈希映射的键!!)

      5) 现在您可以将数字四舍五入到一定的精度并输出num + yourHash[ex]

      一个例子:

      number = 12345.45
      exponent = floor(log10(12345.45))
      
      exponent should now be 4 !
      
      look for a key in the hash map -- whoops no key matches 4 ! -- so take 3 !
      
      set exponent to 3 
      
      now you scale the number:
      
      number = number / pow(10, exponent)
      
      number = 12345.45 / pow(10, 3) 
      
      number = 12345.45 / 1000
      
      number is now 12.34545
      
      now you get the value to the corresponding key out of the hash map
      
      the value to the key, which is 3 in this example, is thousand  
      
      so you output 12.34545 thousand
      

      【讨论】:

        【解决方案4】:

        据我所知,图书馆不支持缩写数字,但您可以自己轻松完成:

        NumberFormat formatter = NumberFormat.getInstance();
        String result = null;
        if (num % 1000000 == 0 && num != 0) {
           result = formatter.format(num / 1000000) + "M";
        } else if (num % 1000 == 0 && num != 0) {
           result = formatter.format(num / 1000) + "K";
        } else {
           result = formatter.format(num);
        }
        

        当然,这假设您不想缩短像 1,234,567.89 这样的数字。如果你,那么这个问题就是duplicate

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多