From the course: Learning C#

String formatting - C# Tutorial

From the course: Learning C#

String formatting

- [Instructor] All right, let's take a deeper look at Formatting Strings. So here in the formatting folder I'm going to open up the program CS file. So far during this course, I've been using the curly brace with index notation to print strings with the values of variables inserted into them. And that's similar to what I have here in my code when I'm printing the strl one variable on this line. Now we can use the same mechanism to get more control over how data is formatted into the string. And the general format for doing this is by specifying the index, which is what we've been doing. And you can see that this is the general format right here. So we specify the index along with an optional alignment value which we'll talk about in a little bit and then a colon and then a format specifier. So we'll take a closer look at the alignment in a moment but for now we'll just focus on the formatting and there are several formatting options available. So for example, I can use N for just a general number or G for general data format, there is F for fixed-point, exponential, decimal, percent, hexadecimal, currency and a few more that are a little bit advanced, I'm not going to use here. So let's try some of these out. I'm going to try printing out Val1 which is an integer using a few of these options. So what I'm going to do is use my console.wright line like I've been using and then inside that string I'm going to create a formatting string. So I'm going to have zero, but this is what I've been doing and then a colon and then I'll specify D four decimal number and then I'll put a comma and space and then I'll just copy and paste a few of these. So we'll do D and then we'll do N and then we'll do F for fixed point and then we'll do G for general and also remove that extra comma and then I'll call this with val1. And then let's do the same thing with Val2, which you can see here is a decimal number. So I'll go ahead and copy this line and paste it. I'll call it with Val2. I'll make the options a little bit different. We'll change D to an E for exponent, leave the N as it is, we'll leave everything else the same, right? Just change the first one to an E. All right, so let's go ahead and run this. So I'll save and then let's bring this up in the terminal and you can see that when I run this, you can see the output how the two different numbers were formatted by the specifiers. So for the decimal, we have just the regular number then we have the number format with a comma for thousands, then the fixed point with no comma and then general, which for integers is the same as D. And then for Val2, here's the exponential notation, here's again the number with the comma and here's fixed point and then general and in this case, you can see that the decimal's included. So in addition to specifying the format, you can add what's called a precision specifier to the format string. So let's go ahead and do this for Val1. So what I'm going to do is copy this line and paste it down here. So right after the letter, I'm going to add an integer numbers. So for example, for D I'm going to use six to indicate that I want the number to take up six spaces, for N I'm going to specify two, for F I'll use one and for G I'll use three. So let's go ahead and run this again. And now in the output you can see that for D6, it uses six spaces including some leading zeros, for N2, it specifies two decimal places, which is what we had before. For F1 that shortens it to just one decimal place and for G3, it uses just three digits and then puts the rest in the exponential format. Let's go back and take a look now at how the alignment specifier works. So I've got some other variables in my code here and these variables represent some sample sales data over four quarter periods, along with the percentage of sales that were international instead of domestic. We're going to use string formatting to format this data in a nice, easy to read layout. So let's start by formatting the quarter headings. So what I'll do, is all console.wright line and I'll build my specifier stream and I'll have my zero like I had before. And instead of putting in a colon what I'm going to do is put a comma and then the number 12. So I want this column to take 12 spaces and I'm going to do that for each one of these. So I'll copy that, actually I want the space too, so I'm going to to copy that with the space and then paste, paste, paste and get rid of the last comma and set that to be one and then two and then index three and then I'm going to insert the data from the quarters array. So I'll just specify quarters zero and I'll just fill this in or just one, two and three. All right, so that's one, two and three, oops. Okay, let's run the updated code And, Oh, whoops, you know, I put commas in there, didn't want to put commas, let me take the comments out. Just spaces. All right, let's run it again. And now you can see, Oh, I took it out of the wrong string, nevermind. I'll take it out of here, there we go, spaces, spaces, spaces. All right, let's run it again. All right, so now you can see that each one of these quarters numbers is formatted in a column that is 12 characters wide. So now let's do the same thing for the sales data. So what I'm going to do is just copy this line and of course, instead of quarters, I'm going to use sales. So I'll replace each of these with sales. What I'm also going to do is format each of these as currency. So after the 12, I'll put in the colon and then use the letter C for currency and then I'll specify a numerical precision. So I'll have the first column be zero and the second column also be C0 and then C0 for the third one and then C0 for the fourth one. And then let's go ahead and add the international percentages. So we'll copy and paste one more time and this'll be international mix percent. So I'll copy and paste that. Now I want these to show up as percentages. So instead of C, what I want is P for percentage. And I'll have the first two be P with precision zero and then just to show some variations, we'll have this one be P1, and then we'll have the last one be P2. Okay, let's go ahead and run the code one more time. And now you can see our nicely formatted data table here. So we have the quarter number. You can see that the data is being formatted as currency and then the last row is being formatted as percentages. And the first two numbers have zero decimal points and then this has one and then this has two. So at this point I would suggest maybe taking a few moments here to try some of your own experiments with string formatting to get a feel for how it works. And of course you can read the standard numeric format strings documentation here at this link in the C-sharp docs. So if you scroll down, you'll see there's a whole bunch of details on each one of these format specifiers. So go ahead and take some time, try some of your own formatting ideas and then refer it back to these docs as you need to.

Contents