如何缩短 BigInteger 值?
How can I shorten a BigInteger value?
我正在尝试将 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 不能存储足够大的值,所以我必须使用 BigInteger
s。如何使用 BigInteger 以这种方式格式化它?
在我的例子中,它应该处理的最大数量是:
1 000 000 000 000 000 000 000 000 000 and is formatted to 1.000B
编辑
不,这不是 this post 的副本。我必须使用 BigInteger 才能工作,而且必须使用 BigInteger 来完成。与我需要的相比,长值(正如其他 post 所询问的那样)没有存储足够大的值
除以该值,直到检索到小于 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
从 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行)
我正在尝试将 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 不能存储足够大的值,所以我必须使用 BigInteger
s。如何使用 BigInteger 以这种方式格式化它?
在我的例子中,它应该处理的最大数量是:
1 000 000 000 000 000 000 000 000 000 and is formatted to 1.000B
编辑
不,这不是 this post 的副本。我必须使用 BigInteger 才能工作,而且必须使用 BigInteger 来完成。与我需要的相比,长值(正如其他 post 所询问的那样)没有存储足够大的值
除以该值,直到检索到小于 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
从 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行)