gpt4 book ai didi

java - 从 double 中删除 .0

转载 作者:搜寻专家 更新时间:2023-10-31 08:07:05 26 4
gpt4 key购买 nike

我正在尝试动态显示字符串中的数字,所以如果数字有小数则显示它们,但如果没有则不显示 .0

示例:将 5.5 显示为 5.5,将 5.0 显示为 5

这是我目前所拥有的:(答案是双倍的)

double temp = answer;
long temp2 = (long) temp;
if (temp == temp2) {
output = String.valueOf(temp2);
System.out.println(output);

这项工作在 1e18 之前都没有问题,然后会因为 Long 的最大尺寸而出错。那么我如何在更大的数字(如 5.43e86)上实现这一目标

最佳答案

使用DecimalFormat

double answer = 5.0;
DecimalFormat df = new DecimalFormat("###.#");
System.out.println(df.format(answer));

关于java - 从 double 中删除 .0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7109388/

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