eclipse 试图在java中四舍五入到两位小数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13105847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to round to two decimal places in java
提问by user1718272
The System.printf("%.2f", currentBalance) is working fine but the problem is the rounded number appears after the sentence. Put the code into your eclipse program and run it and you can see something is definitely wrong. If someone could help it would be much appreciated.
System.printf("%.2f", currentBalance) 工作正常,但问题是四舍五入的数字出现在句子之后。将代码放入你的 eclipse 程序并运行它,你可以看到肯定有问题。如果有人可以提供帮助,将不胜感激。
public class BankCompound {
public static void main (String[] args) {
compound (0.5, 1500, 1);
}
public static double compound (double interestRate, double currentBalance, int year) {
for (; year <= 9 ; year ++) {
System.out.println ("At year " + year + ", your total amount of money is ");
System.out.printf("%.2f", currentBalance);
currentBalance = currentBalance + (currentBalance * interestRate);
}
System.out.println ("Your final balance after 10 years is " + currentBalance);
return currentBalance;
}
}
}
采纳答案by sunleo
Please try this
请试试这个
import java.text.DecimalFormat;
public class Visitor {
public static void main (String[] args) {
compound (0.5, 1500, 1);
}
public static double compound (double interestRate, double currentBalance, int year) {
for (; year <= 9 ; year ++) {
System.out.println ("At year " + year + ", your total amount of money is "+Double.parseDouble(new DecimalFormat("#.##").format(currentBalance)));
currentBalance = currentBalance + (currentBalance * interestRate);
}
System.out.println ("Your final balance after 10 years is " + currentBalance);
return currentBalance;
}
}
回答by Matt Ball
System.out.println()
, as the name implies
System.out.println()
,顾名思义
behaves as though it invokes
print(String)
and thenprintln()
.
表现得好像它调用
print(String)
然后println()
。
Use System.out.print()
and put the newline after printing the current balance.
System.out.print()
在打印当前余额后使用并放置换行符。
System.out.print("At year " + year + ", your total amount of money is ");
System.out.printf("%.2f", currentBalance);
System.out.println();
// or
System.out.print("At year " + year + ", your total amount of money is ");
System.out.printf("%.2f\n", currentBalance);
回答by Darren
System.out.printf ("At year %d, your total amount of money is %.2f\n", year, currentBalance);
System.out.printf("在第 %d 年,您的总金额为 %.2f\n", year, currentBalance);
回答by Abhishek
Faulty call is the very first System.out.println() as it appends a new line after printing the given content.
错误的调用是第一个 System.out.println() ,因为它在打印给定内容后追加一个新行。
There are two solutions-
有两种解决方案——
Approach -1 :
方法-1:
System.out.print("At year " + year + ", your total amount of money is ");
System.out.printf("%.2f\n", currentBalance);
Approach -2 : [Using String.format() with println()]
方法 -2 : [使用 String.format() 和 println()]
System.out.println ("At year " + year + ", your total amount of money is "
+ String.format("%.2f", currentBalance));
Both will produce the same result. Even the second one is more readable.
两者都会产生相同的结果。即使是第二个也更具可读性。
Output:
输出:
At year 1, your total amount of money is 1500.00
在第 1 年,您的总金额为 1500.00
At year 2, your total amount of money is 2250.00
在第 2 年,您的总金额为 2250.00
At year 3, your total amount of money is 3375.00
在第 3 年,您的总金额为 3375.00
At year 4, your total amount of money is 5062.50
在第 4 年,您的总金额为 5062.50
At year 5, your total amount of money is 7593.75
在第 5 年,您的总金额为 7593.75
At year 6, your total amount of money is 11390.63
在第 6 年,您的总金额为 11390.63
At year 7, your total amount of money is 17085.94
在第 7 年,您的总金额为 17085.94
At year 8, your total amount of money is 25628.91
在第 8 年,您的总金额为 25628.91
At year 9, your total amount of money is 38443.36
在第 9 年,您的总金额为 38443.36
Your final balance after 10 years is 57665.0390625
10 年后您的最终余额为 57665.0390625
String.format returns a formatted string. System.out.printf also prints the formatted string on system.out(console).
String.format 返回一个格式化的字符串。System.out.printf 还会在 system.out(console) 上打印格式化的字符串。
Use them as per your needs.
根据您的需要使用它们。