【问题标题】:How can I shorten a BigInteger value?如何缩短 BigInteger 值?
【发布时间】:2016-10-05 05:31:12
【问题描述】:

我正在尝试将 BigInteger 数字格式化为更易于阅读的形式

例子:

1000 -> 1.000K

5821 -> 5.821K

10500 -> 10.500K

101800 -> 101.800K

2000000 -> 2.000M

7800000 -> 7.800M

92150000 -> 92.150M

123200000 -> 123.200M

1 000 000 000 000 000 000 -> 1.000E

1 000 000 000 000 000 000 000 -> 1.000Z

1 000 000 000 000 000 000 000 000 -> 1.000Y

611 781 555 431 000 000 000 000 000 -> 611.781Y

我看到了一个使用 long 值的方法,但出于我的目的,long 无法存储足够大的值,所以我必须使用 BigIntegers。如何使用 BigInteger 以这种方式对其进行格式化?

在我的情况下,它应该处理的最大数量是:

1 000 000 000 000 000 000 000 000 000 并被格式化为 1.000B

编辑
不,这不是 this post 的副本。我必须使用 BigInteger 才能工作,并且必须使用 BigInteger 来完成。与我需要的相比,长值(正如另一篇文章所询问的那样)没有存储足够大的值

【问题讨论】:

  • 我解释了为什么这是错误的。他们使用 long,为了我的目的,我必须使用 BigInteger。 Long 无法存储足够大的值。编辑帖子以更清晰地添加该细节。
  • 9223372036854775807 大得多?您的所有示例都适合long10000000000000000000 的预期结果是什么?
  • 1234567 的预期结果是什么?应该是1.234m、1.234567m还是1234.567k?
  • 罗伯特考克:1.234m。 Tunaki:添加了很多例子

标签: java biginteger


【解决方案1】:

将该值相除,直到检索到小于 1000 的值。
根据除法的次数确定后缀(K、M 等)。
使用最后除法的余数确定小数。

例如:

public String formatNumber(BigInteger value)
{
  // Initialize suffixes
  String[] suffixes = new String[]{null, 'k', 'M', ....};

  // Initialize divider
  BigInteger thousand;
  thousand = new BigInteger(1000);

  // Initialize divisions
  BigInteger final_value;
  BigInteger remainder;
  int        nr_divisions;
  final_value = value;
  remainder = null;
  nr_divisions = 0;

  // Divide until final value less then 1000
  BigInteger[] division;
  while (final_value.compareTo(thousand) >= 0)
  {
    division = final_value.divideAndRemainder(thousand);
    final_value = division[0];
    remainder = division[1];
    nr_divisions++;
  }

  // Maybe no divisions
  if (nr_divisions == 0)
    return (value.toString());

  // Determine suffix
  // Some check required since number of divisions may exceed the number of suffixes provided
  String suffix;
  suffix = suffixes[nr_divisions];

  // Compose string
  return (final_value.toString() + "." + String.format("%03d", remainder.intValue()) + suffix);

} // formatNumber

【讨论】:

  • 我的意思是你应该使用后缀“K”,以防你除以 1000 只用一次原始值。将其除以 1000 两次,后缀为“M”。
  • 如果要添加一些代码,这个算法可能是一个有用的答案。
  • 我编辑了答案,提供了一种可能的算法。
【解决方案2】:

从 BigInteger 到字符串:How to convert locale formatted number to BigInteger in Java?

之后你需要这样的东西(需要改进):

public static void main(String[] args) throws Exception {
    print("1.034");
    print("21.034.234");
    print("1.034.234.321");


}

private static String convert(String points) {
    String[] letters = new String[]{"k", "M", "G"};
    int size = points.length();
    String after = points;
    if (size > 3) {
        int firstPoint = points.indexOf(".");
        after = points.substring(0, firstPoint + 4); 
        int x = (size - firstPoint - 3)/4;
        after += letters[x];
    }
    return after;
}

static void print(String x) {
    System.out.println(x + " is " + convert(x));
}

写一段代码真的没那么难(比如不到 20 行)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 2015-12-31
    • 2016-02-18
    • 1970-01-01
    相关资源
    最近更新 更多