Next, we will learn to control the number of decimal places that are used to display a value. Picking up where we left off last time, we saw that if we simply use %f as an insertion point, by default, Matlab gives us six decimal places. You can easily override that.
Let's do an up arrow to get our command back. In order to specify the number of decimal places you wish the number to use, we have to place some special characters between the percent sign and the F. This is really very straightforward and obvious. You put a dot and then you put the number of decimal points you want to. So period two.
This is insertion point. The point two says I want two decimal places. The F says I want fixed point format. So let's try that and see what happens. And we get a 7.00.
That's exactly what we wanted. But actually, that's probably not really what we want. We probably want an integer here, since it's difficult to say the poodle boofed 3.9 times.
That's going to be an integer. So we probably want zero decimal places. So we can place a zero there.
There we go. The poodle boofed seven times. While we're at it. Let's look at one more type of numeric format.
Right here, instead of an F format for fixed point, I'm going to put a G, which stands for general. If you put a G, MATLAB will decide what the best way to display this is. If the number is an integer, it will display it as an integer.
If it's a really big or really small number, it will go to scientific notation using an But if you place a G there, it will say the poodle poofed seven times. Let's define one more thing here. This is going to be mon mass equals 369.85 semicolon.
Now we've got mon mass defined. Let's put in another f print f statement here. The monster's mass is percent 0.1 f kilograms. Note the use of two single glitches to get the...
apostrophe in the text string. Remember that. We've seen that before. Here we want it printed out with one decimal place, and we're going to insert the value of mon mass there. Let's run that, see what happens.
We get the monster's mass is 369.9. Now do note that it did round to a 0.9. It was 0.85 up here, so it does round off. It does not truncate.
It rounds. Let's see. one more type of formatting control that we can use before we move on.
I'm going to come in here. I use the up arrow to get my command back. Let's place between the percent and the dot a three.
Okay, well, that seems pretty obvious what that's going to do, so let's try it and see. The monster's mass is 369, three digits, 0.9 kilograms. Let's change it one more time.
The monster's mass is, let's make that a five. Well, that's exactly the same. It didn't seem to have any effect. Let's try one more thing.
Let's drop that down to one and see. Well, that's exactly the same too. One more. Let's change that digit before the dot to a nine.
Ah! That's different. You have to be careful here because your first thought just looking at this it looks like I want three places before the dot and one after. I want five places before the dot and one after.
That is not what this means. What this means is this is the minimum number of print locations that MATLAB will use to print the number. If you specify a number that is too small, like 1, it will use more, but it will never use less than the number between the percent sign and the dot. Now note, this number requires a total of five print positions. 1, 2, 3, 4, 5. Therefore, any number 5 or less is going to give us exactly the same output.
However, if I say I want to use nine spaces. We come along, the monster's mass is, then we have a space, and then to print that number from mon mass, we have to utilize 9. spaces total even if we don't really need them so there's the is there's the space right there would be where that percent sign starts so that's one two three four five six seven eight nine a total of nine print positions were used now you say well why the heck would you want a big space like this if you were printing this same Senate over and over and over on successive lands in Some values had, say, one digit to the left of the decimal point, and some had three, some had five. It allows you to align all of these values in a nice column, so it looks a little prettier. One more thing. Let's get that back, and I'm going to add one more thing right here before that nine, and that is a negative sign.
Wait a minute. How can you have negative print positions? That's not what What that means, it does not mean negative, if you place a dash in front of that number, whatever it is, and press return, it left justifies that number instead of right justifies it. The extra spaces appear to the right instead of to the left.
So that allows you to align numbers in a column if you're printing the same sentence with different values over and over, either so that they're all aligned on the right or so that they're all aligned on the left. In our next presentation, we will continue our coverage of F.F. We've still got a long way to go.