Formatting Strings Using String Format

Be aware that the use of the .NET string formatting characters are not limited to console programs! These same flags can be used when calling the shared String.Format() method. This can be helpful when you need to build a string containing numerical values in memory for use in any application type (Windows Forms, ASP.NET, XML web services, and so on). To illustrate, update Main() with the following final code:

' Now make use of some format tags.

Sub Main()

' Use the shared String.Format() method to build a new string.

Dim formatStr As String formatStr = _

String.Format("Don't you wish you had {0:C} in your account?", 99989.987) Console.WriteLine(formatStr) End Sub

Figure 3-6 shows the output for the formatting logic used by the current application.

Source Code The BasicConsoleIO project is located under the Chapter 3 subdirectory.

ImMT

H5 C:\Windows\system32\cmd.exe

***** Fun with Console 10 ***** C format : $99,989.99 D9 format : 000099999 E format: 9.999977E+004 F3 format: 100000.000 N format: 99,999.00 X format: 1869F x. format: 1869f

Don't you wish you had 599,989.99 "in your account? Press any key to continue . . .

Figure 3-6. The System.Console type in action

Was this article helpful?

0 0

Post a comment