VOOZH about

URL: https://www.geeksforgeeks.org/cpp/floating-point-manipulating-default-format/

⇱ Floating point Manipulating Default Format - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Floating point Manipulating Default Format

Last Updated : 11 Feb, 2026

In C++, floating-point values are printed using a default format, but it can be customized for precision, style, and rounding using stream manipulators. These tools help control how numeric output appears.

  • Manipulators like setprecision, fixed, and scientific change the display format of numbers.
  • They are useful for producing clear, readable, and well-formatted numeric output.

Floating-Point Stream Manipulators in C++:

1. Changing Precision with setprecision(n):

setprecision(n) function lets you control the number of significant digits displayed when printing floating-point numbers. By default, the precision is set to 6 digits, but this can be increased or decreased as needed.


Output
123.5
123.4568

Explanation: setprecision(4) and setprecision(7) allow 4 and 7 significant digits to be printed respectively.

2. Showing or Hiding Trailing Zeros with showpoint():

By default, trailing zeros after the decimal point are omitted to keep the output compact. The showpoint() function forces the display of trailing zeros, while noshowpoint() reverts back to the default behavior.


Output
50
50.0000
50

Explanation: showpoint(): forces the display of trailing zeros after the decimal point, so the output becomes 50.0000, whereas noshowpoint(): hides the trailing zeros and restores the default compact format, so the output becomes 50.

Note: In the showpoint() output, only 4 zeros appear after the decimal because the default precision is 6 significant digits, so 50 already uses two digits and the remaining four are shown as zeros.

3. Displaying Positive Signs with showpos():

By default, only negative numbers are shown with a sign (-). The showpos() function explicitly displays the + sign for positive numbers, while noshowpos() disables it.


Output
10 -10
+10 -10
10 -10

Explanation: showpos forces the + sign to appear before positive numbers (+10), while noshowpos removes it and restores the normal output.

4. Changing Exponential Notation Case with uppercase():

When using scientific notation, the e in the output is lowercase by default. The uppercase() function converts it to uppercase (E), while nouppercase() reverts this change.


Output
1.234568e+04
1.234568E+04
1.234568e+04

Explanation: scientific prints the number in scientific notation, uppercase changes 'e' to 'E' , and nouppercase changes it back to lowercase 'e'.

Comment
Article Tags:
Article Tags: