Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用自定义格式将收到的长数字格式化为十进制数字_Java - Fatal编程技术网

Java 使用自定义格式将收到的长数字格式化为十进制数字

Java 使用自定义格式将收到的长数字格式化为十进制数字,java,Java,我收到一个格式化为长的数字。虽然我收到这个数字很长时间了,但我现在知道最后3位数字对应一个小数部分,所以我想显示用分组和小数分隔符格式化的数字 示例:如果我收到编号11111111我希望它显示为111111.111 我有以下代码: DecimalFormat formatter = new DecimalFormat(); DecimalFormatSymbols symbols = new DecimalFormatSymbols(); formatter.setGro

我收到一个格式化为长的数字。虽然我收到这个数字很长时间了,但我现在知道最后3位数字对应一个小数部分,所以我想显示用分组和小数分隔符格式化的数字

示例:如果我收到编号
11111111
我希望它显示为
111111.111

我有以下代码:

    DecimalFormat formatter = new DecimalFormat();
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    formatter.setGroupingUsed(true);
    symbols.setDecimalSeparator('.');
    symbols.setGroupingSeparator(' ');
    formatter.setDecimalFormatSymbols(symbols);

    long valueAsLong = 11111111111L;
    double value = (double) valueAsLong / 1000;

    System.out.println(formatter.format(valueAsLong));
    System.out.println(formatter.format(value));

我想知道我是否可以在不使用强制转换的情况下实现这一点,也就是说,设置一个接收
long
的格式化程序,并按照我想要的方式格式化数字。

无十进制格式不支持这一点,因为它的目的是将数字格式化为字符串,而不更改其值

格式(longValue/1000.0)
是最简单的解决方案,但请注意,它不适用于非常大的long:

public class Test {
    public static void main(String[] args) {
        DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.US);
        char decimalSeparator = decimalFormat.getDecimalFormatSymbols().getDecimalSeparator();

        // prints 123.456
        System.out.println(decimalFormat.format(123456 / 1000.0));

        // 9,223,372,036,854,775,807
        System.out.println(decimalFormat.format(Long.MAX_VALUE));

        // 9,223,372,036,854,776, not 9,223,372,036,854,776.807, as double's resolution is not sufficient
        System.out.println(decimalFormat.format(Long.MAX_VALUE / 1000.0));

        // 9,223,372,036,854,775.807
        BigInteger[] divAndRem = new BigInteger(Long.toString(Long.MAX_VALUE))
                .divideAndRemainder(new BigInteger("1000"));
        System.out.println(decimalFormat.format(divAndRem[0]) 
                + decimalSeparator + divAndRem[1]);

        // using String manipulation
        String longString = decimalFormat.format(Long.MAX_VALUE);
        System.out.println(new StringBuilder(longString).replace(
                longString.length() - 4, 
                longString.length() - 3, 
                Character.toString(decimalSeparator)));
    }
}

实际上,您可以不用强制转换就可以执行此操作,但需要使用
“#\u2030”
模式进行解析,其中
\u2030
(每千)字符

    long myLong = 123456L;
    String asPerMille = 
       new StringBuffer().append(myLong).append('\u2030').toString();

    DecimalFormat perMilleFormat = new DecimalFormat("#\u2030");        
    Number myLongAsDec = perMilleFormat.parse(asPerMille);

    System.out.println(
        String.format("%d can be parsed with pattern %s as a per-mille and gives %f", 
        myLong, 
        perMilleFormat.toPattern(), 
        myLongAsDec.doubleValue()));
输出为:

123456 can be parsed with pattern #‰ as a per-mille and gives 123.456000

请注意,就性能而言,您的方法肯定比构建和解析一个
字符串
,然后用不同的分组对其重新格式化要好。

我认为您应该考虑以下内容:。所需的输出应该是双倍的,因此您必须将您的输入转换到它。但也可以通过自动装箱来实现:double
value=valueAsLong/1000.0