在java中如何把1200的格式化为1.2k的格式?

無名 发表于: 2021-08-31   最后更新时间: 2021-09-13 09:49:09   1,845 游览

因为我的数字太大,希望将数字转换成千(k),这些易读的单位。

如何用java实现,如下,将数字格式化为右边易读的数字?

1000 转成 1k
5821 转成 5.8k
10500 转成 10k
101800 转成 101k
2000000 转成 2m
7800000 转成 7.8m
92150000 转成 92m
123200000 转成 123m

右边的数字是长整数或整数,左边的数字是字符串。

其他要求:

  • 格式最多应为 4 个字符

  • 1.1k 可以,11.2k 不行。 7.8m 相同,19.1m 不行。小数点前只允许一位数有小数点。小数点前两位表示不是小数点后的数字。

  • 没有必要进行四舍五入。(附加了k和m的数字更多的是模拟量,表示近似值,而不是逻辑的精确。因此,四舍五入是不需要的)

发表于 2021-08-31

这是一个适用于任何 long 值的解决方案,我发现它非常易读(核心逻辑在 format 方法的底部三行中完成)。

它利用 TreeMap 找到合适的后缀。 它比我之前编写的使用数组并且更难阅读的解决方案更有效。

private static final NavigableMap<Long, String> suffixes = new TreeMap<> ();
static {
  suffixes.put(1_000L, "k");
  suffixes.put(1_000_000L, "M");
  suffixes.put(1_000_000_000L, "G");
  suffixes.put(1_000_000_000_000L, "T");
  suffixes.put(1_000_000_000_000_000L, "P");
  suffixes.put(1_000_000_000_000_000_000L, "E");
}

public static String format(long value) {
  //Long.MIN_VALUE == -Long.MIN_VALUE so we need an adjustment here
  if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1);
  if (value < 0) return "-" + format(-value);
  if (value < 1000) return Long.toString(value); //deal with easy case

  Entry<Long, String> e = suffixes.floorEntry(value);
  Long divideBy = e.getKey();
  String suffix = e.getValue();

  long truncated = value / (divideBy / 10); //the number part of the output times 10
  boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10);
  return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix;
}

测试代码:

public static void main(String args[]) {
  long[] numbers = {0, 5, 999, 1_000, -5_821, 10_500, -101_800, 2_000_000, -7_800_000, 92_150_000, 123_200_000, 9_999_999, 999_999_999_999_999_999L, 1_230_000_000_000_000L, Long.MIN_VALUE, Long.MAX_VALUE};
  String[] expected = {"0", "5", "999", "1k", "-5.8k", "10k", "-101k", "2M", "-7.8M", "92M", "123M", "9.9M", "999P", "1.2P", "-9.2E", "9.2E"};
  for (int i = 0; i < numbers.length; i++) {
    long n = numbers[i];
    String formatted = format(n);
    System.out.println(n + " => " + formatted);
    if (!formatted.equals(expected[i])) throw new AssertionError("Expected: " + expected[i] + " but found: " + formatted);
  }
}
你的答案

查看java相关的其他问题或提一个您自己的问题