0

How can I round the first few even numbers to two zeros?

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 );

            }
         }
      }

Currently this gives me the output of:

 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

I assume once the decimals are fixed on the first four, the tabbing will right itself.

I AM NEW TO JAVA! I do not know complicated things.. and was wondering if there was an easy fix that didn't add on much code at all.

Thank you!

  • 3
    `DecimalFormat` might be helpful – Dennis Meng Sep 26 '13 at 23:31
  • 1
    This is not really rounding - it is a matter of display format. http://stackoverflow.com/questions/2538787/how-to-display-an-output-of-float-data-with-2-decimal-places-in-java – sashkello Sep 26 '13 at 23:32
  • I am not sure how to use that in my code. I have looked it up.. but I get errors whenever I try to add it in. –  Sep 26 '13 at 23:32
  • This is not helpful at all. I cannot figure out how to use it. I tried: System.out.println( a + "\t" + b + "\t" ); System.out.print( "\t" ); System.out.printf( "%.2f", c); System.out.printf( "%.2f", d); Which happened to give me some error.. and not print out in proper formatting –  Sep 26 '13 at 23:37
  • Always use BigDecimal to represent money. See my answer below. – stepanian Sep 27 '13 at 00:00

3 Answers3

2

You can try formatted string like in C:

System.out.printf("%d\t%d\t%.2f\t%.2f\n", a, b, e, f);
yafrani
  • 521
  • 4
  • 9
  • the output still looks like my original example, except the decimals are right.. i need the last numbers lined up under the "income adjusted for inflation".. also some are tabbed over and some are not –  Sep 26 '13 at 23:42
  • @user2808966 try following: `System.out.printf("%d\t%d\t%.3f\t\t%.2f\n", a, b, e, f);` – Smit Sep 26 '13 at 23:44
  • Thank you smit! That fixed everything! Is there a resource where I can learn about what printf and all the % data is? –  Sep 26 '13 at 23:46
  • 1
    @user2808966 you mean like a tabular... then try something like this: `System.out.printf("%d\t%d\t%10.2f\t%10.2f\n", a, b, e, f);` – yafrani Sep 26 '13 at 23:47
  • @user2808966 Look [--> Formatting Numeric Print Output <--](http://docs.oracle.com/javase/tutorial/java/data/numberformat.html) **AND** [--> Java printf( ) MethodQuick Reference <--](http://web.cerritos.edu/jwilson/SitePages/java_language_resources/Java_printf_method_quick_reference.pdf) – Smit Sep 26 '13 at 23:51
  • This answers the OP's literal question, but fails to point the OP in the right direction. Doubles and integers should NEVER be used to represent money in Java. Always use BigDecimal. – stepanian Sep 27 '13 at 00:11
2

You should use BigDecimal to represent money.

See:

Representing Monetary Values in Java

https://blogs.oracle.com/CoreJavaTechTips/entry/the_need_for_bigdecimal

http://www.javapractices.com/topic/TopicAction.do?Id=13

You can use the various methods that BigDecimal has for scaling, rounding, formatting, etc.

Community
  • 1
  • 1
stepanian
  • 11,373
  • 8
  • 43
  • 63
0

Have a look at printf:

http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

It's better suited to printing out formatted data. You won't even need to round it yourself!

Cramer
  • 1,785
  • 1
  • 12
  • 20