0

I am currently trying to get my program to round to 2 decimal places, but seem to be unable to. Here is my code:

double ounce;
double grams; 
grams = 0.00;
System.out.println("This programme will print a table that can be used to convert ounces to grams, from values 1-15.");
System.out.println("One ounce is equal to 28.35 grams.\n\nOunces \t\t\t Grams");

ounce = 0;
do {
  ounce++;
  grams+=28.35;
  System.out.println((""+ounce+"\t\t\t"+grams+""));
} while (ounce <15);

The output looks like this:

    Ounces           Grams
1.0                  28.35
2.0                  56.7
3.0                 85.05000000000001
4.0                  113.4
5.0                  141.75
6.0                  170.1
7.0                  198.45
8.0                  226.79999999999998
9.0                 255.14999999999998
10.0                 283.5
11.0                311.85
12.0                340.20000000000005
13.0                368.55000000000007
14.0                 396.9000000000001
15.0            425.2500000000001

Lastly, my numbers under "grams" are not aligning exactly proper. Any ideas how to get them to align perfectly?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Aarden Hartle
  • 39
  • 1
  • 4
  • 10
  • That output doesn't seem to come from that code, as `gramsFormated` is not changed in your loop. What's the real code? – RealSkeptic Oct 24 '14 at 22:30
  • I am not sure what you mean. I could try and change it, but this is the code. Upon looking at this code, I realise I could simply just use "grams" and not "gramsFormated", if thats what you mean. **edit** I edited my code slightly, but the output has not changed. – Aarden Hartle Oct 24 '14 at 22:34
  • What I meant was that you didn't paste the code that actually produced the output you pasted. With `grams` it makes more sense. If it was `gramsFormatted`, then with this code it would have printed the same value in all the rows. Anyway, now you are not formatting it at all. – RealSkeptic Oct 24 '14 at 22:37

2 Answers2

4

You can use a another output method like this:

           System.out.format("%f%5f\n",ounce, gramsFormated);

You can decide how you want your width. In this example it is 5.

Mosa
  • 373
  • 1
  • 14
  • Unfortunately, this does not work for me as I need each line of code to be on a separate line, not one line horizontally. – Aarden Hartle Oct 24 '14 at 22:38
  • 1
    Oh sorry. Use this: System.out.format("%f%5f\n",ounce, gramsFormated); – Mosa Oct 24 '14 at 22:39
  • It seems that in line `System.out.format("%f%5f\n",ounce,"\t\t\t",grams,"");` the "\t\t\t" is causing problems with this way of formatting. I am not sure if there is a way to fix this... – Aarden Hartle Oct 24 '14 at 22:44
  • No don't do it like this. Do it exactly like in the answer: System.out.format("%f%5f\n",ounce, gramsFormated); – Mosa Oct 24 '14 at 22:53
  • You can find more information about formating here: http://docs.oracle.com/javase/tutorial/java/data/numberformat.html – Mosa Oct 24 '14 at 22:54
3

If you wish to properly align as well as round to the correct number of digits, you can use the System.out.format method, as Mosa suggested.

System.out.format("%5.2f%20.2f%n", ounce, grams);

Note:

  • The format does all the alignment for you, there is no need to add tabs.
  • The accepted way to write a newline inside a format string is %n rather than \n.
  • The first part %5.2f says: Print the first parameter in a field 5 characters wide, with two digits after the decimal point.
  • The second part %20.2f does the same for the second parameter, only it defines the field as 20 characters wide. This also creates the spacing between your two columns. You can, of course, change that width.
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79