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

标签 java

从此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/

相关文章:

Java 线程程序无法使用 wait() 和 notifyAll()

JavaFX java.lang.IllegalArgumentException : Can not set javafx. scene.control.Label 字段 sample.Controller.location 到 java.net.URL

java - 从整数列表的索引中打印字符串

java - JNI 和取消 AsyncTask

java - 没有一定数量数字的新列表/数组?

java - XmlBeans.Factory 解析方法的 ClassCastException 不一致

java - 让tomcat在重启时启动的问题

java - Eclipse Text Search,自动循环搜索词列表?

java - 如何在java中使用正则表达式分割字符串

java - 有谁确切知道 javax.jms.InvalidDestinationException : Not allowed to create destination means? 是什么