In this video,
we'll see some of the stream formatting options available for floating point numbers
formatting. Floating point numbers focuses mainly on the amount of
precision used to display the number.
By default, the precision is set to 6 digits.
We'll cover the specifics of what that exactly means in this lecture.
We can also set the number of digits to display to the right side of the decimal point.
By default, there is no fixed set size.
We can also choose to show trailing 0s up to a certain number,
and we can also decide if the floating point number 4 should be displayed as 4
or 4.0 or 4.00 and so forth.
But by default, these trailing 0s are not displayed.
And then we have some of the same options that we had when we used integers.
By default, a lowercase e is used when we're displaying floating point numbers
using scientific notation.
And the plus sign is not displayed when we display positive floating point numbers.
All of these manipulators persist once they're set.
So first let's take a look at how precision works.
By default, the precision used to display floating
point numbers is 6 digits and rounding always occurs.
In this example, we have the number
1234.5678.
If we display this number at c++, we'll use
6 as the default precision
and start at the most significant digit which is the 1,
and it will display 6 digits starting from there rounding at the end.
In this case, 1234.57 will be displayed.
If c++ plus can't display the number in the given amount of significance,
then it'll use scientific notation, and we'll see that in a bit.
Here's another example. We have a large number
123456789.987654321,
I did it that way just so it's really easy to keep track of these digits.
If we display this number,
c++ plus will try to do it with 6 significant digits, but this won't work
since it will display 123456,
and the number displayed wouldn't be accurate.
So in this case, the number we displayed using scientific notation.
And notice 1.23457 has got the 6 digits
of precision that's the default.
And notice that we're still using precision 6, but this time
using scientific notation.
Suppose we wanted to display the number using precision 9.
We can use the set precision manipulator where the 9 is the argument to do just that.
In this case, the number will display as 1234567 90.
Note the rounding at the end.
Also notice how no trailing 0s are displayed in this example.
So that's the way precision works
when we're not fixing the number of digits to the right of the decimal.
When we use the fixed manipulator, precision is handled differently.
Now we're looking at precision starting from the right side of the decimal point.
In this case, we still have a default precision of 6,
but we're also using the fixed manipulator.
This will use a precision of 6 starting with the digits after the decimal,
and we'll round as necessary.
So in this case, exactly 6 digits are displayed after the decimal point,
and 0s are added if necessary.
As you can guess, set precision 2 and fixed
are commonly used to display currency amounts.
We actually did this in a few of the course lessons.
In this example, we're still using that very large number,
but we're setting the precision to 3 and also using the fixed manipulator.
So now this will display exactly 3 digits after the decimal point
and rounding will occur.
So we've got a 0.988 after the decimal point.
In this example, we want to set precision to 3 and also use
scientific notation.
You can see the manipulators being used and the display is exactly as we expect,
1.23 times 10 to the 8th.
Notice the precision is 3, we see that with the 1.23.
We can display the e in the scientific notation format as a capital e
by using the upper case manipulator.
Remember, by default, it's no uppercase.
Now let's see how the show positive manipulator works with floating point numbers.
The behavior is the same as we saw with integers.
If we use the showpos manipulator,
then we'll get a preceding plus sign on positive floating point numbers.
Note that we can toggle back to not showing the plus sign
with the no show pause manipulator.
With floating point numbers, we can also use the show point manipulator.
This will display trailing 0s based on the level of precision used.
For example, if our number is 12.34,
and we display it using only the defaults, then 12.34 will display.
We're using a precision of 6,
but only 4 digits will display in this case.
If we wanted to add trailing 0s up to precision,
we can use the show point manipulator.
Now you can see that 2 0s are added to the number being displayed.
Again, you can use the no show point to toggle back to the default behavior.
Finally, if you want to reset the floating point format back to the general format,
you can do that in 1 of 2 ways. You can use the unset f method
or you can use the reset ios flag manipulator.
There are a bunch of other flags, but at this point,
you know what you would need. So rather than list them all here,
I'll refer you to the c++ reference docs.
Now let's head over to the IDE, and we'll format some floating point values.
Then in the next video, we'll finish off formatting
with setting field widths justification and filling.
Okay. So I'm back in the IDE again.
And I'm still in the section 19 workspace. And I'm in the manip
_floating project.
And what we'll do here is we'll play around with some floating point numbers.
Now let's take a look at the numbers that we'll be using for this example.
We've got 3 numbers right here. They're all doubles.
And you can see I've got 1 that's going from 1 up to 9 and then from 9 down to
1 after the decimal point,
that'll just make it really easy for us to see which digits are being rounded.
I've got another one that's a little smaller 1234.5678.
And I've got this last one right here 1234.0.
Okay. And what we'll do first is we'll just do normal
displaying these guys out to the console with absolutely
nothing fancy, no manipulation at all, just
using pure defaults. And that's what's going on right here.
On lines 14 to 16, I'm just playing num1 num2 num3.
Now remember, by default, we've got 6 digits of precision.
We're not showing the positive sign.
We're not showing trailing 0s,
and we're not showing uppercase letters and scientific notation.
Okay. That's the default.
So let's run this first. Now what do we expect here. We expect num1
to not be able to work, right. I mean if you think about this for a second here,
you can see that num1 has -- let's assume we wanted to display 6
significant figures right here.
We would just play 123456, and that would
really give us problems,
right, because that's not even close to being an accurate number.
So c++ won't do that.
What it'll do is it'll display that number in scientific notation,
and it will still use 6 for precision. I'll show you how that works in a second.
Now this guy can be, right. We've got -- there's 4 digits right here, and then 2.
So we expect those 6 digits to display, and those last 2 will be rounded.
And this guy here, remember by default,
we don't display trailing 0s, so that's just gonna display 1234.
So let's run this, and we'll verify that we got what we expected.
Okay. So let me move this window over here so we can see what's going on.
Let me do that again. There we go. So in this case, we're displaying num1,
right. And notice what happens. 1.23457, you can see the rounding,
you can see all 6 digits being displayed here.
But we're using scientific notation because there's no way that it can be
properly displayed or accurately displayed I should say in 6 digits.
This next one though num2 right 1234.57,
that's exactly what we expect.Here you see the 6 digits. And then num3,
1234, it's displayed. There's no .
0 or .00 unless we use a modifier which we'll use in a little bit.
So that's the default. And it's really important that you understand the default
and how it begins.
So that's using the default settings. Now let's try something. Let's
-- we're going to set the precision to 2.
So what we're asking for is 2 significant digits.
They can't do that with any of these,
right. 1.2, where's it going to stop.
So what it'll do is it'll display all 3 of them in scientific notation
using a precision of 2. So I'll show you how that looks.
And you can see exactly what's going on,
1.2 for all of them. Those are the 2 significant digits of precision, right.
Times 10 to the 8h to 3rd and so forth.
So now let's set a precision of 5.
Now we'll be able to do some of them with a precision of 5, but
others it won't. So I'll uncomment this block out.
And let's execute that.
And now you can see what's going on. Num1
cannot be displayed in 5 significant digits here.
So the precision is going to be 5 digits here, but it's going to be in scientific notation.
The other number up here, I'll scroll up so you can see it, 1234.567,
there are your 5 significant figures here, right. Your precision is 5.
So what it's doing is it's rounding that fifth one from -- which is point 0.56 to 6.
And then again the last one is 1234, we're not adding any 0s or anything.
So that's the default kind of behavior. So it really gives you an idea of what's going on.
In this case, we're going to set precision to 9.
Everything will fit here. So let's try that.
And you can see what's happening with precision 9.
I get 1234567,
instead of 89, I'm getting 90 because it's rounding that.
This guy's displaying all of it because it can display all of it,
right 1234.5678.
And the 1234 still displays the same way because we're not adding any 0s.
Okay. So now you understand precision.
Now let's talk about fixed and how it changes precision.
So what we're doing here is we're setting precision to 3.
You can see that happening right here on line 40.
And we're fixing the decimal point.
So now the precision happens after the decimal point.
Okay. So let's run this and take a look at the output.
Okay. So take a look at the output here.
Again, I'll scroll up so we can see these numbers up top.
So now we're displaying 123456789,
right. We're displaying all of that. And the precision now applies
after the decimal.
So notice that all 3 of these guys are displaying 3
digits after the decimal because that's the precision,
and we're fixing that size. So it's going to round. In this case,
9876 becomes 988,
and 5678 becomes 568.
And in this case here, you can see that it's displaying all the 0s on the end,
okay, because we set the fixed formatter.
All right. So hopefully that makes sense. Let me stop that, and we'll run
a couple more. Let's take a look at this example.
What we're doing here is we're setting the precision to 3,
and we want to do a scientific notation.
So this should be very similar to what happened before with the precision 2 when stuff wouldn't fit.
So again, precision is 3 and scientific notation.
So what we get is right down here
1.235. Notice the precision is 3, right.
But what happened because it's fixed, I set fixed up here
and fix is persistent. So it carries down even though I didn't say fixed right here,
it's fixed because I've set it up here.
So in this case, I get the 3 decimals
after the decimal point, the 3 digits, I should say, after the decimal point,
and it's all in scientific notation. And then a couple more examples.
You notice that e in the exponential notation,
in scientific notation was a lowercase e.
I can make it a capital e by simply using std uppercase here.
And let's just run that to be sure that we get it.
And there you go. Right down here at the bottom you can see 1.23,
and you can see the e is capitalized here, right. It was lowercase here,
in the previous example.
In this case, I'm using the show positive.
And what I'm doing is I'm just setting it to precision 3 fixed
and show positive. So this will show a positive
plus sign in front of positive numbers.
Let's run that. And then you can see down here at the bottom again.
I'll move this up just a little so you can see it a little better.
You can see the plus symbol being displayed in front of all these positive floating point numbers.
If they were negative, obviously the negative sign would show.
Now we can set these guys back to the defaults
either by doing an unset f calling that method,
and we can pass in scientific and fixed sending them back to the defaults.
And what we're doing here is just a binary or,
or we could use the reset ios flags
manipulator here and pass in show position, and that'll unset that.
All right. So let me clean this up here.
Now notice what I'm doing here, I'm setting precision to 10,
and I'm telling it to show point. Okay. What this is going to do is going to add
these trailing 0s up to 10 spaces.
All right, or up to 10 precision. So let's run that.
And we can see num1, num2 and num3.
There you go. You can see right here 1234567890,
it's rounding here.
And then 1234.5678, we've got a couple of 0s on the end.
But you notice, all of them now are in precision 10, right.
And in this case, we have 1234 point and then six 0s at the end.
We'll set these guys back to the defaults. And I'll show you how to do that right here.
And then we'll run one more time we should get that same behavior
we got at the very beginning.
So what I'm doing here is I'm setting this back to the general notation here.
I'm setting the precision to 6, which was the default.
And I'm setting show positive and
show point back to their defaults.
And then I'm just displaying num1, num2, num3 at the end.
And what we should get back is what we got at the beginning.
So down at the bottom, here you go. We get the scientific notation for this guy.
We've got the 6 precision digits here, and we've got the 1234 right here.
Okay. So that's it that's. A good example of
these floating point formatters.
They're pretty powerful, they're pretty easy to use.
In the next video what we'll do is we'll learn about setting field widths and justifying things left to right,
and you can use those along with these to really format tables and get your format
looking really nicely for output.