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 '/'

Recommended Answers

All 2 Replies

basically align the $

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.