gpt4 book ai didi

java - 在 Java 中确定百万、十亿、万亿、千万亿

转载 作者:行者123 更新时间:2023-12-04 14:42:32 32 4
gpt4 key购买 nike

从此Standard dictionary numbers ,我需要一种最快的方法来转换下面的一些数字:

1000000 = 100 万
1435234 = 143 万
350000000 = 3.5亿
1000000000 = 10 亿
1765000000 = 17.6亿
1000000000000 = 1 万亿
1345342345000 = 1.34 万亿
1000000000000000 = 1 万亿
100000000000000000 = 100 万亿

还有更多。

我尝试过这样的:

public String truncateNumber(float floatNumber) {
long million = 1000000L;
long billion = 1000000000L;
long trillion = 1000000000000L;
long number = Math.round(floatNumber);
if ((number >= million) && (number < billion)) {
float fraction = calculateFraction(number, million);
return Float.toString(fraction) + "M";
} else if ((number >= billion) && (number < trillion)) {
float fraction = calculateFraction(number, billion);
return Float.toString(fraction) + "B";
}
return Long.toString(number);
}

public float calculateFraction(long number, long divisor) {
long truncate = (number * 10L + (divisor / 2L)) / divisor;
float fraction = (float) truncate * 0.10F;
return fraction;
}

但我认为我的解决方案并不完全正确。那么,在 Java 中最快的方法是什么?非常感谢。

最佳答案

第一个问题是 float没有足够的精度来表示这些数字。事实上,即使 double对 Nonillion 范围内的值没有足够的精度 - 尽管这在这里可能不是那么重要,因为无论如何您显然都想删除这个数字的大部分信息。

不过,我在这里使用 BigInteger 实现了它。 .将其转换为使用 double如果您不关心精度问题,应该是直截了当的。

这里的基本思想是创建一个 NavigableMap从 1000 的幂到相应的数字名称。可以使用 floorEntry 快速查找此 map 找到最佳匹配功率(因此,数字名称)。

import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.TreeMap;

public class NumberNames
{
public static void main(String[] args)
{
test("100", "Nearly nothing");
test("1000", "1 Thousand");
test("1230", "1.23 Thousand");
test("1000000", "1 Million");
test("1435234", "1.43 Million");
test("350000000", "350 Million");
test("1000000000", "1 Billion");
test("1765000000", "1.76 Billion");
test("1000000000000", "1 Trillion");
test("1345342345000", "1.34 Trillion");
test("1000000000000000", "1 Quadrillion");
test("100000000000000000", "100 Quadrillion");
test("1230000000000000000000000000000000000000000000000000000000000000", "1.23 Vigintillion");
}

private static void test(String numberString, String string)
{
BigInteger number = new BigInteger(numberString);
System.out.println(number+" is "+createString(number)+" should be "+string);
}



private static final String NAMES[] = new String[]{
"Thousand",
"Million",
"Billion",
"Trillion",
"Quadrillion",
"Quintillion",
"Sextillion",
"Septillion",
"Octillion",
"Nonillion",
"Decillion",
"Undecillion",
"Duodecillion",
"Tredecillion",
"Quattuordecillion",
"Quindecillion",
"Sexdecillion",
"Septendecillion",
"Octodecillion",
"Novemdecillion",
"Vigintillion",
};
private static final BigInteger THOUSAND = BigInteger.valueOf(1000);
private static final NavigableMap<BigInteger, String> MAP;
static
{
MAP = new TreeMap<BigInteger, String>();
for (int i=0; i<NAMES.length; i++)
{
MAP.put(THOUSAND.pow(i+1), NAMES[i]);
}
}

public static String createString(BigInteger number)
{
Entry<BigInteger, String> entry = MAP.floorEntry(number);
if (entry == null)
{
return "Nearly nothing";
}
BigInteger key = entry.getKey();
BigInteger d = key.divide(THOUSAND);
BigInteger m = number.divide(d);
float f = m.floatValue() / 1000.0f;
float rounded = ((int)(f * 100.0))/100.0f;
if (rounded % 1 == 0)
{
return ((int)rounded) + " "+entry.getValue();
}
return rounded+" "+entry.getValue();
}
}

关于java - 在 Java 中确定百万、十亿、万亿、千万亿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24434555/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com