gpt4 book ai didi

java - string.format 自动填充我的 double 如何让它停止

转载 作者:行者123 更新时间:2023-12-02 07:12:42 28 4
gpt4 key购买 nike

示例输入:

double a = 31.3131313131313131313131313131313131131313131313131313131313131;
String value1 = String.format("N= %1$39.32f", a);
System.out.println(value1);

示例输出: N= 31.3131313131310000000000000...

当有需要的值时,为什么我的程序会自动填充?我如何控制它,因为我希望调用这些值。

谢谢

最佳答案

这是 double 的问题,看看这个:

public static void main(String[] args) {
double a = 31.3131313131313131313131313131313131131313131313131313131313131;
System.out.println(Double.toString(a));
}

输出:

31.31313131313131

java double 数据类型无法容纳这么高的精度。请改用 BigDecimal

public static void main(String[] args) {
BigDecimal a = new BigDecimal("31.3131313131313131313131313131313131131313131313131313131313131");
System.out.println(a.toPlainString());
}

输出:

31.3131313131313131313131313131313131131313131313131313131313131

关于java - string.format 自动填充我的 double 如何让它停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15322540/

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