Calculations / Rounding & Formatting Functions
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Now that we've discussed how to handle different data types and the importance of being consistent with your data types, you also want to be able to display the results of your calculations in a very particular way. When you want to display numeric data of a text property of a label or a text box, you must first convert the value to a string. You can do this in a number of ways. One way is to use what is referred to as format specify our codes. These codes are one letter and then you can specify other options within that code. For example, you can format data to be in currency format or fixed-point format, number format or percent format. Using our sample program, let's visit the Code Window and you'll notice I have modified the last line of code to include a code of C that's preceded by the method to string. This says to convert the answer, which is calculated in the line above, to a string using the currency codes. C stands for currency. In all of these codes that we will look at, the default is two decimal places. So if you want two decimal places to appear, you do not need to specify a two; however, you could and it would not hurt anything. But this will display the data in currency format with two decimal places. So let's run this to see how this looks. So if I enter 15.2 times 13 you will see that it will display a dollar sign and two decimal points. This is what the currency format will display. Now, let's stop the program and return to the Code Window and I've now added additional lines of code to show how different codes will be displayed. As you can see, there is an F code, an N code and a P code. The F code stands for fixed point and like the currency, by default it will display two decimal places. If you would like something other than two decimal places to be displayed, you can specify a one after the F and that will give you fixed-point decimal, a one-point decimal. The next example is an N, which stands for number and again, this will supply two decimal points. The difference between number and fixed point is basically the use of commas and we'll see how that plays out when we have a number which is big enough. And then finally the third example is P3, which is going to display the data in percent format with three decimal places. Once again, the default would be two decimal places if I did not specify a number. Back on the form I've added some labels so that we have a place for each one of these to go. So let's calculate the program and I'll enter 15.9 times 13 and as you can see, now we have the same result displayed five different ways. The first example is currency format. The second example is fixed with a default of two decimal places. Third example is fixed with one decimal point. The fourth example is number, the N code and by default it will display two decimal places. And the last example is the percent. You notice the percent sign and there are three decimal places because that's what I asked for. Now, if I modify the second number to provide a larger result, you now see the difference between the fixed and the number. Once again, currency will display dollars and cents with two decimal points, but notice the use of the comma. Currency will display a comma if a number goes to the thousands position. The next two examples do not display a comma. That is the fixed format. The fourth example is the number or the N format. That does display the comma but it does not display a dollar sign like the currency does. And then the third example, the percent, as you can see, does display commas as well. One more modification to provide some more decimal points; here you can see how the rounding occurs when you specify one decimal point. The .18 is getting rounded to .2. So rounding will automatically occur according to how many decimal places you request. Keep in mind that the default is always two decimal places with these format codes. One more look at the format is simply where you want the answer to be displayed, the variable that's being displayed and then to string parenthesis, the code inside of quotation marks, parenthesis closed. By the way, these codes can be uppercase or lowercase letters. For example, uppercase C or the lowercase c would work equally well. Same is true with the others.
Tutorial Information
| Course: | Microsoft Visual Basic 2005 |
| Author: | Arthur Lee |
| SKU: | 33940 |
| ISBN: | 1-935320-10-6 |
| Release Date: | 2008-11-19 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 