Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Thinking in C++ Vol 2 - Practical Programming
Prev Home Next

Width, fill, and precision

The internal variables that control the width of the output field, the fill character used to pad an output field, and the precision for printing floating-point numbers are read and written by member functions of the same name.



int ios::width( )

Returns the current width. Default is 0. Used for both insertion and extraction.

int ios::width(int n)

Sets the width, returns the previous width.

int ios::fill( )

Returns the current fill character. Default is space.

int ios::fill(int n)

Sets the fill character, returns the previous fill character.

int ios::precision( )

Returns current floating-point precision. Default is 6.

int ios::precision(int n)

Sets floating-point precision, returns previous precision. See ios::floatfield table for the meaning of precision.

The fill and precision values are fairly straightforward, but width requires some explanation. When the width is zero, inserting a value produces the minimum number of characters necessary to represent that value. A positive width means that inserting a value will produce at least as many characters as the width; if the value has fewer than width characters, the fill character pad the field. However, the value will never be truncated, so if you try to print 123 with a width of two, you ll still get 123. The field width specifies a minimum number of characters; there s no way to specify a maximum number.

The width is also distinctly different because it s reset to zero by each inserter or extractor that could be influenced by its value. It s really not a state variable, but rather an implicit argument to the inserters and extractors. If you want a constant width, call width( ) after each insertion or extraction.

Thinking in C++ Vol 2 - Practical Programming
Prev Home Next

   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire