gpt4 book ai didi

java - 我如何让它返回一个 double 而不转换为字符串

转载 作者:行者123 更新时间:2023-11-30 06:55:31 25 4
gpt4 key购买 nike

public double futureInvestmentValue(int years) {
DecimalFormat dfWithTwoDecimalPlaces;
dfWithTwoDecimalPlaces = new DecimalFormat("0.00");
double futureInvestmentValue = deposit * Math.pow((1 + (AnnualInterestRate / 12)), years * 12);
return dfWithTwoDecimalPlaces.format(futureInvestmentValue);

我收到的错误是在最后一行。它说:“类型不匹配:无法从 String 转换为 double”它要求我更改为公共(public)字符串。

谢谢!

最佳答案

在您的方法签名中,您将方法的返回类型声明为 double 型。

但是,这一行:

return dfWithTwoDecimalPlaces.format(futureInvestmentValue);

调用一个返回字符串的方法。您必须决定此函数的用途,以及您是否需要它返回预先格式化的值或将此责任留给调用者。

关于java - 我如何让它返回一个 double 而不转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35304804/

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