473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Aligning dollar amounts in printf statements.

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
1 5084
Banfa
9,065 Expert Mod 8TB
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

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

Similar topics

1
by: Kevin | last post by:
Group By - Client Sum - PastDue: Iif(DateDiff("d",,Date())>30,,0) Sum - Current: Iif(DateDiff("d",,Date())<31,,0) These 2 sum fields work fine, EXCEPT it is not summing my negative...
2
by: Paul E Johnson | last post by:
Dear friends in C: I'm completely baffled by this problem I have with printf statements and conditional operators in C. I'm using gcc-3.3. I have a project where the rarely used Union type...
9
by: bdog4 | last post by:
I want to validate a number to so that they can only input $2.00, 5.00, 7.00, 50.00 etc... I don't wan them to be able to put cents on the amount. It can either either be 2 or 2.00 Any ideas on...
3
by: mark.a.lemoine | last post by:
I need to be selecting records based on the value of a string-format currency value. Specifically, I have a table with a field of type varchar(50). Stored in this field is a dollar-formatted...
7
by: tshad | last post by:
I need to convert a number I get from my Sql table (where the value is a string) to a string and have it be a precision of 2 and I want to reverse the sign. I originally used: ...
14
by: Harman Dhaliwal | last post by:
Hi, I am programming an in memory representation of a database and am at the tail end, hopefully, of implementation. But the vexing point is that my program runs fine with all my diagnostic...
6
by: shaqattack1992-newsgroups | last post by:
Hello Everyone, I'm using the following on a form in my database: =(-Int(-Sum((IIf(=Yes,((*)+()), (((*)+)*1.06))*100))))/100 In this case, I want to calculate a total for an order. If...
19
by: RedDevilDan | last post by:
I am working on a Memory Footprint Reduction project. I came across an idea to disable all printf statements so that less memory is required. In addition, when there is no single printf statement,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.