gpt4 book ai didi

java - 使用 System.out.format() 格式化 float 时,会显示不相关的答案。为什么?

转载 作者:行者123 更新时间:2023-12-03 00:19:40 25 4
gpt4 key购买 nike

谁能解释一下我的下面的代码是如何解释的。

float n = 534343434.66f;
System.out.printf("%13.3f",n);

输出:

534343424.000

为什么会出现这个输出?

enter image description here

最佳答案

Java 中的

Float 大小为 4 个字节。因此,请使用 double 而不是使用 float

double n = 534343434.66;
System.out.printf("%13.3f",n);

关于java - 使用 System.out.format() 格式化 float 时,会显示不相关的答案。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59543118/

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