Formatting Output - fixed, showpoint, setprecision

Deborah S
27 Sept 201604:38

Summary

TLDRThis video demonstrates the use of format manipulators in a simple C++ program. It begins with displaying values of two variables, x and y, then introduces the iomanip library for formatting. The 'fixed' and 'showpoint' manipulators ensure decimal points are always shown. 'Setprecision' is used to control the number of decimal places displayed, which is demonstrated by adjusting precision levels. The video also shows that 'setprecision' affects floating-point values but not integers. The explanation is clear and includes running the program to illustrate the effects of these format manipulators.

Takeaways

  • πŸ“š The script demonstrates the use of format manipulators in a simple program.
  • πŸ”’ Two variables, x and y, are defined with values 123.456 and 987.654, respectively.
  • πŸ’» The program displays the values of x and y using cout.
  • πŸ“˜ To use format manipulators, the iomanip library is included in the program.
  • πŸ”§ Adding 'fixed' and 'showpoint' ensures values are displayed in fixed notation with decimal points.
  • πŸ” 'setprecision' is used to control the number of decimal places displayed.
  • ✏️ Setting precision to 2 rounds the value of x to 123.46 and y remains 987.65.
  • πŸ”„ Precision can be changed at any point in the program.
  • πŸ”’ Setting precision to 1 rounds x to 123.5 and y to 987.7.
  • βž– Setting precision to 0 displays the decimal point without any decimal places.
  • πŸ”’ Adding an integer variable z with a value of 10 shows that 'setprecision' does not affect integers.

Q & A

  • What are the variables used in the example program?

    -The variables used in the example program are x and y.

  • What does the program initially display?

    -The program initially displays the value of x and y, which are 123.456 and 987.654 respectively.

  • What library needs to be included to use format manipulators?

    -The IO manip library needs to be included to use format manipulators.

  • Why is the 'fixed' manipulator used in the program?

    -The 'fixed' manipulator is used to ensure that the values are displayed in fixed notation, avoiding exponential notation.

  • What is the purpose of the 'showpoint' manipulator?

    -The 'showpoint' manipulator ensures that the decimal point is always displayed, even for values like 4.0.

  • How does 'setprecision' affect the output?

    -'setprecision' sets the number of decimal places for the output. For example, setting precision to 2 will display two places after the decimal.

  • What happens when the precision is set to 2 for the initial values of x and y?

    -When the precision is set to 2, the value of x is rounded to 123.46 and the value of y is displayed as 987.65.

  • Can the precision be changed at different points in the program?

    -Yes, the precision can be changed at different points in the program to affect subsequent outputs.

  • What is the output when the precision is set to 1 for y?

    -When the precision is set to 1 for y, it is displayed as 987.7.

  • Does 'setprecision' affect integer values?

    -'setprecision' does not affect integer values; it only works with floating-point values.

  • What is the value of the integer variable z in the program?

    -The value of the integer variable z in the program is 10.

Outlines

00:00

πŸ”§ Demonstrating Format Manipulators

This paragraph introduces a simple program to demonstrate format manipulators in C++. It starts with defining two variables, x and y, and prints their values. The program then introduces the iomanip library to include format manipulators, explaining that each library must be on separate lines without semicolons. The fixed and showpoint manipulators are used to ensure values display in fixed notation and show decimal points. The precision is set to ensure only two decimal places are shown. The program also demonstrates changing the precision during runtime and explains that setprecision does not affect integers.

Mindmap

Keywords

πŸ’‘format manipulators

Format manipulators are tools in programming that control the display format of output values. They ensure that numbers are shown in a consistent way, such as fixed decimal points or scientific notation. In the video, the use of format manipulators like 'fixed' and 'showpoint' ensures that numeric output is displayed in a readable and predictable format.

πŸ’‘fixed

The 'fixed' format manipulator forces the output to display numbers in fixed-point notation, preventing them from being shown in scientific notation. This is important in the video to ensure that values like 123.456 and 987.654 are displayed as regular decimal numbers, not with an exponent.

πŸ’‘showpoint

The 'showpoint' format manipulator ensures that the decimal point is always shown, even if the number is a whole number. In the video, using 'showpoint' makes sure that a number like 4.0 is displayed as '4.0' instead of just '4', maintaining clarity in numerical output.

πŸ’‘setprecision

'setprecision' sets the number of digits to be displayed after the decimal point for floating-point numbers. In the video, 'setprecision(2)' limits the output to two decimal places, such as displaying 123.456 as 123.46.

πŸ’‘iostream

The 'iostream' library in C++ includes functionality for input and output operations. It is mentioned in the video as the fundamental library needed for basic I/O tasks, such as displaying values using 'cout'.

πŸ’‘iomanip

The 'iomanip' library in C++ provides manipulators for controlling the format of input and output. The video demonstrates including 'iomanip' to use manipulators like 'fixed', 'showpoint', and 'setprecision' to format the output.

πŸ’‘variable

A variable in programming is a storage location identified by a memory address and an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. In the video, variables 'x' and 'y' are used to store floating-point numbers that are formatted and displayed.

πŸ’‘floating-point numbers

Floating-point numbers are numbers that contain a fractional part, represented by decimal points. In the video, variables 'x' and 'y' store floating-point values (e.g., 123.456), which are formatted for output.

πŸ’‘integer

An integer is a whole number that does not contain any fractional part. In the video, the variable 'z' is declared as an integer with a value of 10 to demonstrate that format manipulators like 'setprecision' do not affect integer variables.

πŸ’‘precision

Precision in the context of floating-point numbers refers to the number of digits displayed after the decimal point. The video illustrates changing the precision using 'setprecision', which affects how many decimal places are shown in the output.

Highlights

Introduction to format manipulators in programming

Demonstration of a simple program with variables x and y

Displaying the values of x and y using cout

Inclusion of the IO manip library for format manipulators

Explanation of the importance of separating libraries in preprocessor directives

Use of fixed and show point to control the display of floating-point values

Fixed notation prevents values from being displayed in exponential form

Show point ensures the decimal point is displayed in values

Impact of set precision on the number of decimal places displayed

Example of setting precision to two decimal places

Demonstration of rounding behavior with set precision

Changing precision during the program to one decimal place

Observation of precision effect on integer values

Inclusion of the integer manipulator and its effect on variable Z

Demonstration of set precision having no effect on integers

Final display of variable Z with no decimal point due to integer type

Transcripts

play00:02

in this example I'm going to demonstrate

play00:04

format manipulators here's a very simple

play00:07

program I have two variables x and y you

play00:10

can see the values stored in there I'm

play00:12

going to see out X is it's going to

play00:14

display the value of the variable X and

play00:17

down to the next line and display Y is

play00:19

the value of the variable Y and the

play00:22

program is going to end here's what it

play00:24

looks like

play00:32

okay X is one two three point four five

play00:34

six y is nine eight seven point six five

play00:38

four okay now I'm going to add some

play00:41

format manipulators when you have any

play00:45

format manipulators you need to add a

play00:47

new library in your program so I'm going

play00:49

to include

play00:50

IO manip that's the name of the library

play00:54

notice you can have more than one

play00:56

library up here if you do each library

play00:59

has to be on different lines right

play01:01

separate lines never any semicolons at

play01:04

the end of these preprocessor directives

play01:07

and the order is not important now most

play01:10

of the time what programmers do is right

play01:12

before there see out statements they'll

play01:14

put this line in here

play01:18

okay and what fixed in show point two is

play01:22

they will ensure that the values that

play01:24

get displayed are first of all in a

play01:26

fixed notation that means you won't see

play01:29

any values with an exponent in them like

play01:31

e to the plus O to the values that I'm

play01:35

using by default won't go into that

play01:37

exponential notation but if you use

play01:40

values with more significant digits or

play01:42

larger values they might so you always

play01:44

want to use fixed and show point make

play01:47

sure that your decimal point displays if

play01:49

you have a value like four point zero it

play01:52

will only display for by default unless

play01:54

you have show point in there so by

play01:56

putting this one line right prior to

play01:58

your C out statements where you display

play02:01

your results it'll make sure that that's

play02:03

an effect for the rest of the program

play02:05

next I'm going to demonstrate set

play02:08

precision

play02:10

so I'm going to set the precision to two

play02:13

just because that was my choice and set

play02:16

precision will set the number of decimal

play02:18

places for everything from this point on

play02:21

number of decimal places will be two

play02:24

right two places after the decimal

play02:26

notice our values one two three point

play02:29

four five six and nine eight seven point

play02:31

six five four let's run this program and

play02:33

see what happens now

play02:41

notice two places after the decimal the

play02:44

first value one two three point four was

play02:47

rounded up right because it was four or

play02:49

five six and the second value point six

play02:52

five was left alone because the value

play02:54

after the five is four so it's not good

play02:56

around that value up you could change

play02:58

the precision at any point in your

play03:00

program I'm going to come down here I'm

play03:02

going to set my precision

play03:06

to

play03:08

one

play03:12

and I'm going to run it again so when x

play03:16

comes out it should have a precision of

play03:18

two and then down here this will change

play03:20

Y to a precision of one

play03:26

and you'll see that's exactly what

play03:29

happened okay let's just see what

play03:31

happens when we change your position to

play03:34

zero

play03:49

all right you'll see this you get the

play03:51

decimal point but you don't get the zero

play03:53

okay and one last thing I'm going to do

play03:56

is I'm going to put it integer in here

play03:57

and Z is assigned a value of 10 and at

play04:02

the end of my program I'll display Z as

play04:05

well

play04:13

and let's see what happens now keep in

play04:18

mind Z is an integer

play04:27

notice what happens with Z step

play04:29

precision has no effect on integer it

play04:32

only works with floating point values

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
C++ ProgrammingFormat ManipulatorsCoding TipsSet PrecisionIO ManipFixed NotationShow PointProgramming TutorialCode FormattingOutput Control