| Answer: |
One of the nice things about VBScript was its built-in formatting functions, suchas FormatCurrency, FormatNumber, FormatDateTime,etc. (For more information on these functions see thisFAQ or, better yet, check out the StringsFAQ Category.)
When creating ASP.NET Web pages with VB.NET, you can still use these functions, althoughyou will need to import the Microsoft.VisualBasic namespace. But whatif you are using C#, or want to start using the new formatting methods available in.NET? To allow for formatting, the .NET Framework contains a general String.Format staticmethod along with .ToString() methods for each object.
So, first off, you can easily convert a non-string data type to a string by usingit’s .ToString method. That is, if you wanted to turn a DateTime variableinto a string you could do: >
'Create a var. named rightNow and set it to the current date/time Dim rightNow as DateTime = DateTime.Now Dim s as String 'create a string
s = rightNow.ToString()
|
This simple code snippet creates a DateTime variable (rightNow) thatis assigned the current system date/time. A String variable (s) is thencreated and assigned the string representation of the DateTime variable.
The .ToString() contains an overloaded variant that accepts a singleString parameter. This parameter can be used to specify how to format the DateTimevariable. For example, if we wanted to display the date as a three-lettered monthnamethen the day, and then a comma followed by the year in four digits (like Jan 30,2002), we could use the .ToString(formatString) method like so:
'Create a var. named rightNow and set it to the current date/time Dim rightNow as DateTime = DateTime.Now Dim s as String 'create a string
s = rightNow.ToString("MMM dd, yyyy")
|
Pretty neat, eh? Of course the question still remains: “How in the world did I knowto use MMM to display the three-letter abbreviation of a month? The answer,of course, is to hit the documentation. One thing the .NET Framework has an abundanceof is documentation. For a list of special formatting characters for date/time purposes,see DateTimeFormatInfoClass.
Using the overloaded .ToString() method is nice especially when you wantto apply a custom format. However there’s another way to format through the use ofthe String class, which contains a Format method. This method takes aformat string followed by one to many variables that are to be formatted. The formatstring consists of “placeholders,” which are essentially locations to place the valueof the variables you pass into the function. These placeholders assume the form:
{placeholderNumber:formatCharacter}
So if we wanted to format a number into a currency, we could do so by using the followingcode:
'Create a var. named price that will be formatted as a currency Dim price as Double = 3.1 Dim s as String 'create a string
s = String.Format("{0:c}", price)
|
This would output: $3.10 (or perhaps something else, depending on your Web server’sglobal locale information – that is, in China it may display a Yen symbol insteadof the dollar sign). In any case, note that the format string contains a placeholder {0:c},which says to make the 0th variable in the list a currency (the c denotingcurrency). If we wanted to format multiple variables in one shot we could do so:
s = String.Format("{0:c} on {1:d}", price, rightNow)
|
would display:
$3.10 on 1/30/02
Note how the first digit in each placeholder specifies what variable in the proceedinglist to use. Again, you may be wondering how I knew a c would apply acurrency format, or a d would display the date as it did. Again, viathe docs. See StandardNumeric Format Strings and Dateand Time Format Strings for more information.
The following small ASP.NET Web page demonstrates using the String.Format methodto apply some formatting to various variables.
<script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e) { double price = 4.56; DateTime rightNow = DateTime.Now; int bigNumber = Int32.MaxValue;
lblPrice.Text = String.Format("{0:c}", price); lblTime.Text = String.Format("{0:T}", rightNow); lblDate.Text = String.Format("{0:d}", rightNow); lblBigInt.Text = String.Format("{0:#,###}", bigNumber); } </script>
<html> <body>
The price is: <asp:label runat="server" id="lblPrice" /> <p> The time is: <asp:label runat="server" id="lblTime" /> <p> The date is: <asp:label runat="server" id="lblDate" /> <p> The biggest 32-bit integer is <asp:label id="lblBigInt" runat="server" /> </body> </html>
|
The output for the above Web page would be:
The price is: $4.56
The time is: 12:14:52 PM
The date is: 1/19/2002
The biggest 32-bit integer is 2,147,483,647
|
Happy Programming!
|