By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 428,759 Members | 1,727 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

# Aligning dollar amounts in printf statements.

 P: 1 I new to c programming and was wondering if someone can please help me to understand how to get the following printf statements to align up the dollar amounts when displayed? printf ("\n\n*** Bank Record ***\n\n"); printf ("\n Starting Balance: \$ %2.2f \n\n", initial_balance); for (x = 0; x < number_of_deposits; x++) { printf ("\n Deposit #%i: %2.2f \n\n", x + 1, deposits[x]); } for (x = 0; x < number_of_withdrawals; x++) { printf ("\n Withdrawal #%i: %2.2f \n\n", x + 1, withdrawals[x]); } printf ("\n\ Ending Balance: \$ %2.2f \n\n\n\n", balance); Oct 29 '06 #1
Share this Question
1 Reply

 Expert Mod 5K+ P: 8,916 When you give the print format as %2.2f the value before the decimal point gives the number of characters in total to print and the value afterwards gives the number of digits of precision (or 2 decimal places). A value with 2 decimal places uses at least 4 characters (e.g. 0.05 has 4 characters) so by saying field width 2 with 2 decimal places you have specified an impossible format and the field width is ignored. Your field width needs to be higher, how high depends of the largest value you are printing but for 100s \$ it would need to be 6 (%6.2f) for thousands 7 (%7.2f) Then your decimal places will line up. Oct 30 '06 #2

### Post your reply

Sign in to post your reply or Sign up for a free account.