[英]Trying to format double data types with system.out.printf in java
I'm attempting to format a series of calculations into a table. 我正在尝试将一系列计算格式化为表格。 So far I've gotten string data types to work, but double data types seem to behave differently. 到目前为止,我已经使用了字符串数据类型,但是双精度数据类型的行为似乎有所不同。 I get the format conversion errors when the program is run. 程序运行时出现格式转换错误。 I'm new to java, and any help would be greatly appreciated. 我是java的新手,任何帮助将不胜感激。 I am trying to get it to look like this: 我试图让它看起来像这样:
RATE YEAR AMOUNT ON DEPOSIT INTEREST
---- ---- ----------------- --------
0.07 1 1,070.00 70.00
2 1,144.90 144.90
3 1,225.04 225.04
4 1,310.80 310.80
5 1,402.55 402.55
6 1,500.73 500.73
7 1,605.78 605.78
8 1,718.19 718.19
9 1,838.46 838.46
10 1,967.15 967.15
Here is my code: ( I know that the loop is off if it's going to look like the above. I'm just trying to figure out how to format doubles first.) 这是我的代码:(我知道如果循环看起来像上面的那样,循环就关闭了。我只是想弄清楚如何首先格式化双精度格式。)
import java.text.DecimalFormat;
public class interest
{
public static void main(String[] args)
{
DecimalFormat money = new DecimalFormat("$#,##0.00");
DecimalFormat percent = new DecimalFormat("0.00");
double interest = 0;
double principal = 1000;
double rate7 = 0.07;
double rate8 = 0.08;
double rate9 = 0.09;
double rate10 = 0.10;
double endYear = 0;
int yearCount = 1;
System.out.printf("%1s%7s%20s%10s", "Rate", "Year", "Amount on Deposit" , "Interest");
System.out.println();
System.out.printf("%1s%7s%20s%10s", "----", "----", "-----------------", "--------");
System.out.println();
boolean firstTime =true;
while (yearCount <= 10)
{
if(firstTime)
{ //the issue is here
System.out.printf("1f%7d%20f%10f", "percent.format(rate7)", "yearCount","endYear", "interest");
firstTime = false;
}
else
{
endYear = principal * Math.pow((1 + rate7),yearCount);
System.out.println(money.format(endYear));
yearCount++;
interest = endYear - 1000;
System.out.println();
}
}
System.out.format("%1s%7s%20s%10s", "Rate", "Year", "Amount on Deposit" , "Interest");
System.out.println();
System.out.format("%1s%7s%20s%10s", "----", "----", "-----------------", "--------");
System.out.println();
yearCount = 1;
endYear = 0;
interest = 0;
firstTime = true;
while (yearCount <= 10)
{
if(firstTime)
{
System.out.println(percent.format(rate8));
firstTime = false;
}
else
{
endYear = principal * Math.pow((1 + rate8),yearCount);
yearCount++;
interest = endYear - 1000;
System.out.println(money.format(endYear));
}
}
System.out.format("%1s%7s%20s%10s", "Rate", "Year", "Amount on Deposit" , "Interest");
System.out.println();
System.out.format("%1s%7s%20s%10s", "----", "----", "-----------------", "--------");
System.out.println();
yearCount = 1;
endYear = 0;
interest = 0;
firstTime = true;
while (yearCount <= 10)
{
if(firstTime)
{
System.out.println(percent.format(rate9));
firstTime = false;
}
else
{
endYear = principal * Math.pow((1 + rate9),yearCount);
yearCount++;
interest = endYear - 1000;
System.out.println(money.format(endYear));
System.out.println();
}
}
System.out.format("%1s%7s%20s%10s", "Rate", "Year", "Amount on Deposit" , "Interest");
System.out.println();
System.out.format("%1s%7s%20s%10s", "----", "----", "-----------------", "--------");
System.out.println();
yearCount = 1;
endYear = 0;
interest = 0;
firstTime = true;
while (yearCount <= 10)
{
if(firstTime)
{
System.out.println(percent.format(rate10));
firstTime = false;
}
else
{
endYear = principal * Math.pow((1 + rate10),yearCount);
yearCount++;
interest = endYear - 1000;
System.out.println(money.format(endYear));
System.out.println();
}
}
}
}
Replace 更换
System.out.format("%1s%7s%20s%10s", "Rate", "Year", "Amount on Deposit" , "Interest");
with 与
System.out.printf("%s %7d %20f %10f", percent.format(rate7), yearCount, endYear, interest);
As you passing strings not numbers. 当您传递字符串而不是数字时。 Also, for the first string element, you are not specifying '%s'. 另外,对于第一个字符串元素,您未指定'%s'。 Once you replace it, your code will work fine 替换后,您的代码即可正常运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.