0

System.out.printf("\nRegular pay:$ %.2f",regularPay)
System.out.printf("\nOvertime pay:$ %.2f",overtimePay)
System.out.printf("\nTotal Pay:$ %.2f",overtimePay+regularPay);

to print like
/Regular Pay: $
Overtime Pay: $
///Total Pay: $
ignore the '/'

Edited by Fernando_1

2
Contributors
2
Replies
20
Views
4 Years
Discussion Span
Last Post by bguild
1

The simplest solution to align the '$'s would be to merely insert a few spaces, like this:

    System.out.printf("\nRegular pay:    $ %.2f",regularPay);
    System.out.printf("\nOvertime pay:   $ %.2f",overtimePay);
    System.out.printf("\nTotal Pay:      $ %.2f",overtimePay+regularPay);

You could also do something like this which you might prefer:

    System.out.printf("\n%-15s$ %.2f", "Regular pay:", regularPay);
    System.out.printf("\n%-15s$ %.2f", "Overtime pay:", overtimePay);
    System.out.printf("\n%-15s$ %.2f", "Total Pay:", overtimePay+regularPay);

That saves you from having to fiddle with whitespace by hand, but the decimal places don't line up. If you also want the line up the decimal places you can do it like this:

    System.out.printf("\n%-15s$%6.2f", "Regular pay:", regularPay);
    System.out.printf("\n%-15s$%6.2f", "Overtime pay:", overtimePay);
    System.out.printf("\n%-15s$%6.2f", "Total Pay:", overtimePay+regularPay);

The number before the dot specifies the width of the number in characters, with any extra width filled in with whitespace. Or maybe you want something more like this:

    System.out.printf("\n%15s $%6.2f", "Regular pay:", regularPay);
    System.out.printf("\n%15s $%6.2f", "Overtime pay:", overtimePay);
    System.out.printf("\n%15s $%6.2f", "Total Pay:", overtimePay+regularPay);

The '-' controls whether the value is left-aligned or right-aligned. I recommend you read http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html and experiment until you find something that you like.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.