gpt4 book ai didi

java - 如何保存双数中小数点后的数字

转载 作者:行者123 更新时间:2023-11-30 03:45:05 25 4
gpt4 key购买 nike

假设有一些类

public class Currency {

private double value;

public double getValue() {
return value;
}

public Currency(double value) {
this.value = value;
}

@Override
public String toString() {
return value + "";
}
}

重点是:

有浮点变量

double var = 10.000;

和对象

Currency cur = new Currency(var);

我需要方法 toString() 必须返回“10.000”,就像 var 一样(如果小数点后有一系列零,假设它必须在小数点后返回 3 个零),但它返回“10.0”

最佳答案

使用String.format:

    double a = 10;
String s = String.format("%.3f", a);
System.out.println(s);

这是document .

关于java - 如何保存双数中小数点后的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25954020/

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