Dev C++ Printf Float

Posted on

Nov 22, 2019  Skill Level: Intermediate I am sure many of you are familiar with the venerable printf function in C. However, using the cout (standard output stream) object from the iostream standard library is the preferred method for printing in C. Most programmers quickly pick up the basics of using cout, but often get tripped up when trying to format floating point numbers. Integers are great for counting whole numbers, but sometimes we need to store very large numbers, or numbers with a fractional component. A floating point type variable is a variable that can hold a real number, such as 4320.0, -3.33, or 0.01226. The floating part of the name floating point refers to the fact that the decimal point can “float”; that is, it can support a variable. Printing float values with fixed number of decimal places through cout in C Here, we will learn how to print float value with fixed number of decimal places using cout in C program? Cout prints a floating pointer number with a maximum of 6 decimal places (some compilers may print 5 decimal places) by default (without trailing zeros).

  1. C Scanf
  2. C++ Printf %d
  3. Dev C++ Printf Float 2

I have a column of floating point data that I need to right justify and align the decimal points. Help.

C Scanf

Data looks something like this.

C++ Printf %d

30.768
1.345
.430

If you have any questions, comments, or suggestions about our site or our products, please contact us at your convenience. Lumber wizard 5 metal detector with auto tune. 3-year limited warrantyThank you for your interest in purchasing Agri-Fab 45-0345 350-Pound Poly Convertible Push/Tow Dump Cart.

Steinberg

  • 5 Contributors
  • forum 8 Replies
  • 2,081 Views
  • 2 Days Discussion Span
  • commentLatest Postby vegaseatLatest Post

Dev C++ Printf Float 2

Recommended Answers

When in doubt, use the old workhorse printf(). I have played around with manipulators like [php]cout << setfill('0') << setw(8) << d1;
[/php] with results that cause only consternation. The old printf() like Chainsaw mentioned works well:
[php]// right justified numeric output (Dev-C++)

include // may …
Jump to Post

All 8 Replies

Chainsaw12

you could, for example, use printf or sprintf or its ilk. Take a look at the specifications for the strings. You can control right or left justification, leading zeros, number of digits after the decimals, etc.

Dev C++ Printf Float

If your output is using a proportional font, the spaces won't be as wide as the digits, so that can be a problem too; hopefully you will use a non-proportional font, or else you'll have to do placement in some measurement like pixels or inches.