- [Narrator] Next let's take a look
at some numeric formatting.
Now, normally when you display a positive number
it's not preceded by a plus sign
and when you display a negative number
it is preceded by a minus sign.
So let's take a look at how you can force
a positive number to be preceded by a plus sign.
So here we have the number 27 being formatted
in a field of 10 as an integer
by putting the plus sign before the field width
we're saying we would like it to always
display a plus sign if in fact the value
is a positive number.
So in this case we get the formatted string plus 27.
Now, there is also a capability to fill in
the extra spaces with a leading zero
if that's something that would be helpful for you,
so in that case the leading zeroes
would come in inbetween the plus sign and the number,
so as you can see here we've gone ahead,
and actually we haven't done it yet,
let's go ahead and do it, there we go,
we've put in a zero before the 10d
so this is two flags, plus and zero,
and then a field width of 10 indicating
that we're going to format an integer in that field width.
So in this case we'll get a plus sign,
a bunch of zeroes, and then the number 27
in our formatted result.
You also have the ability to indicate
that a space character should appear
wherever the plus sign would normally appear.
So here we have a print statement
that's going to display three separate formatted values,
the value 27 simply as an integer,
the value 27 with a space character
where the plus sign should go,
and we're also going to show you a negative 27 value,
and we put in the space here but that's not going to
have an effect on the minus 27 value.
So in the second case where we said
to put in that space, that's where the plus sign
would normally appear if in fact
we told it to display one, and that enables us
to basically align these two numeric values
in this particular case.
Now, separately we have seen this previously
but I do want to iterate, or revisit it rather,
so here we're using the comma specifier
which is for grouping digits,
and in this case we're formatting an integer
with the thousands separator, so you can see
we get the last three digits, the comma,
the next three digits, the comma,
and the first two digits, and of course
we can use that with floating point values as well,
so here we're formatting a value with two digits
to the right of the decimal point
but we also have that comma flag
to separate at the thousands.