Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.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 NumberFormat.getCurrencyInstance()是否将所有内容都设置为零?_Java_Android_Currency_Number Formatting - Fatal编程技术网

Java NumberFormat.getCurrencyInstance()是否将所有内容都设置为零?

Java NumberFormat.getCurrencyInstance()是否将所有内容都设置为零?,java,android,currency,number-formatting,Java,Android,Currency,Number Formatting,我已经为此工作了几个小时,但找不到解决方案 当我使用此代码时: NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(Locale.US); System.out.println(currencyFormatter.format(12385748375889879879894375893475984.03)); 它给了我输出:$1238574837588990000000000000000000.00 有什么问题

我已经为此工作了几个小时,但找不到解决方案

当我使用此代码时:

 NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
 System.out.println(currencyFormatter.format(12385748375889879879894375893475984.03));
它给了我输出:$1238574837588990000000000000000000.00

有什么问题吗??我给它一个双倍值,它应该能够包含一个比我提供的要大得多的数字,但它给了我所有这些无用的零。。。有人知道为什么以及我能做些什么来修复它吗?

问题不在于您使用的
双精度的大小,而在于精度。一个
double
只能存储15-16位精度的数字,即使它可以存储远大于1016的数字

如果您想要精确的十进制表示,特别是如果您将其用于货币值,则应使用
BigDecimal
。示例代码:

import java.text.*;
import java.math.*;
import java.util.*;

public class Test {
    public static void main(String[] args) {
        NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
        BigDecimal value = new BigDecimal("12385748375889879879894375893475984.03");
        System.out.println(currencyFormatter.format(value));
    }
}
输出:

$12,385,748,375,889,879,879,894,375,893,475,984.03

对于记录,
double
literal 12385748375889879879894375893475984.03的精确值为12385748375889879000357561111150592。

一个double确实可以容纳该大小的数字。不幸的是,它无法保持这种精度

范围是可以用类型表示的最高值和最低值,精度是可以存储的位数

底线是double的范围约为10+/-308,但精度仅为15位小数。网站上也有一些关于这方面的有用信息


作为修复,您应该查看该类型,因为它具有任意精度。

使用以下代码,在netbeas IDE上进行检查,可以正常工作

 NumberFormat currencyFormatter1 = NumberFormat.getCurrencyInstance(Locale.US);
        BigDecimal data= new BigDecimal("12385748375889879879894375893475984.03");
        System.out.println(currencyFormatter1.format(data));

啊,我是个白痴。谢谢你提供的信息。我在这上面浪费了太多时间。