Você está na página 1de 2

Foimatting 0utput in C++

Steinbeig Foimatting Numbeis: Intio to C++ 1


Formatting Integers in C++

0utput to the console can be contiolleu with foimat manipulatois. You must have
the libiaiy:

#i ncl ude <i omani p>

Also, you instiuct the computei to iight justify the output by iunning the statement:

cout << set i osf l ags( i os: : r i ght ) ;

You must now specify the fielu wiuth foi the next output opeiation by using the
set wmanipulatoi with the size of the numbei of columns to be filleu. Then on the
next output opeiation, the value of an integei, iuentifiei, oi integei expiession will
be piinteu on the iight siue of the specifieu fielu.
Theiefoie:

cout << set w( 10) << 100 << set w( 10) << 50 << set w( 10) << 25;

Piouuces

*******100********50********25

wheie each * inuicates a blank space.


Formatting Real Numbers {decimals] in C++

As with integeis, you use set w to specify the total fielu wiuth of each ieal numbei.
Bowevei, if you wish ieal numbeis to be uisplayeu in uecimal foim, you must
specify a fixeu-point foimat with the numbei of positions to the iight of the uecimal.
The following output statement uoes this foi a uesiieu piecision of two places to the
iight of the uecimal:

cout << set i osf l ags( i os : : f i xed | i os : : showpoi nt | i os : : r i ght )
<< set pr eci si on( 2 ) ;

Then

cout << set w( 8 ) << 123. 45;

Piouuces

* * 123. 45


Foimatting 0utput in C++
Steinbeig Foimatting Numbeis: Intio to C++ 2
The expiession set i osf l ags( i os : : f i xed | i os : : showpoi nt | i os : : r i ght ) instiucts
the output stieam to uisplay ieal numbeis in fixeu-point foimat using a uecimal
point. The geneial foim foi using set i osf l ags is:

set i osf l ags( <f l ag
1
> | <f l ag
2
> | | <f l ag
n
> )

Utber flags

i os : : showpoi nt Bisplay uecimal point with tiailing zeios
i os : : f i xed Bisplay ieal numbeis in fixeu-point notation
i os : : sci ent i f i c Bisplay ieal numbeis in floating-point notation
i os : : r i ght Bisplay values iight justifieu

Setprecision

Set pr eci si on specifies the numbei of uigits to the iight of the uecimal point to be
uisplayeu. Set pr eci si on will iemain set until the piogiammei changes it.

Foimatting ieal numbeis causes the following to happen:
1. The uecimal point uses one position in the specifieu fileu wiuth.
2. Tiailing zeios aie piinteu to the specifieu numbei of positions to the iight
of the uecimal
S. Bigits appeaiing to the iight of the uecimal have been iounueu iathei
than tiuncateu.

DANCER: Formatting Errors!!!

As with integeis, if a fielu wiuth is specifieu that is too small, geneially C++will
uefault to the minimum wiuth iequiieu to piesent all uigits to the left of the uecimal
as well as the specifieu uigits to the iight of the uecimal.

Write a Program to find tbe following answers:

Change fixeu-point uecimals to floating-point uecimals with exactly one nonzeio
uigit to the left of the uecimal:
a. 173. 0
b. 12345678. 9
c. - 0. 0000012
d. +014. 768
e. - 5. 2
Change the following floating-point uecimals to fixeu-point uecimals:
a. - 1. 0046E+3
b. 4. 2E- 8
c. 9. 020E10
d. - 4. 615230E3
e. 8. 02E- 3

Você também pode gostar