如何将前几个偶数四舍五入到两个零?
public class MoneyDriver
{
public static void main( String[] args )
{
int a = 2011;
int b = 21;
double c = 2200.00;
double d = 31500.00;
System.out.println( "Year" + "\t" + "Age" + "\t" + "Balance at end of year" +
"\t" + "Income adjusted for inflation" );
for( int r = 1; r < 10; r++)
{
double e = Math.round(c * 100.0) / 100.0;
double f = Math.round(d * 100.0) / 100.0;
System.out.println( a + "\t" + b + "\t" + e + "\t" + f );
a = a + 1;
b = b + 1;
c = ( c + 2000 ) * 1.1;
d = ( d * 1.05 );
}
}
}
目前,这给了我以下输出:
Year Age Balance at end of year Income adjusted for inflation
2011 21 2200.0 31500.0
2012 22 4620.0 33075.0
2013 23 7282.0 34728.75
2014 24 10210.2 36465.19
2015 25 13431.22 38288.45
2016 26 16974.34 40202.87
2017 27 20871.78 42213.01
2018 28 25158.95 44323.66
2019 29 29874.85 46539.85
我假设一旦小数点固定在前四位,制表符就会自行正确。
我是 Java 新手!我不知道复杂的事情......并且想知道是否有一个简单的修复程序根本没有添加太多代码。
谢谢!