Pin Me

Java Programming For Beginners - Part 19


In this article, we discuss how floating-point values are printed in Java. We show what happens when we print them with and without using a “field width".

  • slide 1 of 4

    Printing the values of double and float variables

    We have been using the format specification %d in a printf statement to print the value of an integer variable. If we wish to print the value of a double or float variable, we can use %f. For example, consider:

    double d = 987.654321;

    System.out.printf("%f \n", d);

    The value of d will be printed to a pre-defined number of decimal places (usually 6, but it could vary from one compiler to the next). In this case, the value printed will be 987.654321. However, if d were assigned 987.6543215, the value printed would be 987.654322 (rounded to 6 decimal places).

    Similarly, if x was of type float, its value could be printed using:

    System.out.printf("%f \n", x);

  • slide 2 of 4

    Using a field width

    We just saw that the specification %f prints the number to a pre-defined number of decimal places. Most times, though, we want to say how many decimal places to print and, sometimes, how many columns to use. For example, if we want to print d, above, to 2 decimal places in a field width of 6, we can use:

    System.out.printf("%6.2f \n", d);

    Between % and f, we write 6.2, that is, the field width, followed by a . (point), followed by the number of decimal places. The value is rounded to the stated number of decimal places and then printed. Here, the value printed will be 987.65, which occupies exactly 6 print columns. If the field width were bigger, the number will be padded on the left with spaces. If the field width were smaller, it is ignored, and the number printed using as many columns as necessary.

    As another example, consider

    b = 245.75;

    System.out.printf("%6.1f \n", b);

    In the specification %6.1f, 1 says to round the number to 1 decimal place; this gives 245.8, which requires 5 columns for printing.

    6 says to print 245.8 in 6 columns; since only 5 columns are needed for printing the number, one space is added at the beginning to make up 6 columns, so the number is printed as ~245.8 (~ denotes a space).


    System.out.printf("%6.0f \n", b);

    will print b as ~~~246 (rounded to 0 decimal places and printed in a field width of 6).

    If the specification were %3.1f and the value to be printed is 245.8, it would be printed using 5 print columns, even though the field width is 3. Again, when the field width specified is smaller than the number of print columns required, Java ignores the field width and prints the value using as many columns as needed.

  • slide 3 of 4

    Taking advantage of field width

    We can sometimes use this to our advantage. If we do not know how big a value might be, we can deliberately use a small field width to ensure it is printed using the exact number of print columns (no more, no less) required for printing the value.

    In general, suppose the float or double value v is to be printed with the specification %w.df where w and d are integers. Firstly, the value v is rounded to d decimal places; now, suppose the number of print columns required to print v, including a possible point and a possible sign, is n. Note: There will be no point (.) if d = 0 (the value is to be rounded to a whole number).There are 2 cases to consider:

    If n is less than w (the field width is bigger): The value is padded on the left with (w ‑ n) spaces. For example, if w is 7 and the value to be printed is -3.45 so that n is 5, the number is padded on the left with (7-5) = 2 spaces and printed as ~~-3.45.

    If n is greater than or equal to w (field width is the same or smaller): The value is printed using n print columns. In this case, the field width is ignored.

    As with integers, a field width is useful when we want to line up numbers one below the other. Assume we have three double variables a, b and c with values 419.563, -8.7 and 3.25, respectively. Suppose we want to print the values to 2 decimal places, lined up on the decimal point, like this:




    Since the biggest number requires 6 print columns, we can line them up using a field width of at least 6. The following statements will line them up as above:

    System.out.printf("%6.2f \n", a);

    System.out.printf("%6.2f \n", b);

    System.out.printf("%6.2f \n", c);

    If we use a field width bigger than 6, the numbers will still line up but with leading spaces. For example, if we use a field width of 8, we will get (~ denotes a space)




    Again, we can use one printf instead of three to achieve the same effect:

    System.out.printf("%6.2f \n%6.2f \n%6.2f \n", a, b, c);