asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Base Types > Formatting Base Types

Formatting Base Types

Getting Started   ASP.NET Base Types


Formatting Overview

Formatting in the .NET Framework is implemented using format specifiers and format providers.

A format specifier, or format string, is basically a string of one or more characters that denotes which type of formatting to apply for a given data type. The framework provides a built-in set of format specifiers for use with specific types, though custom format strings can be defined for user-defined types.

A format provider, on the other hand, defines type- and culture-specific formatting rules that govern how string equivalents of specific value types are rendered in different cultures. Likewise, the framework includes a built-in set of format providers, though custom providers can be defined.

Basically, to format a non-string data type, you can just invoke the ToString method of the object to format, and simply pass the desired format specifier, or the desired format provider, or both, as in

object.ToString ( format specifier, format provider )

NOTE: All types that are formattable — that is, types that implement the IFormattable interface — such as numeric, datetime, and enum types, can invoke ToString with a given format specifier and provider.

For example, to format a DateTime object, simply pass the preferred date format specifier, in this case ( "f" ), to the ToString method of the datetime instance. The below illustrates this concept, and displays a string representing the current date and time in the specified format, the exact output of which depends on the notation used by the current culture.

// get current system date and time
DateTime now = DateTime.Now;
Response.Write ( now.ToString ( "f" ) );
  C# VB

Or you can do it simply as shown in our first demo, which is logically the same.

<%= DateTime.Now.ToString ( "f" ) %>

which would render the given datetime instance in a full date pattern, as in

Friday, January 10, 2025 8:02 PM

 Show me 

If no format specifier is given, the default general format ( "G" ) is used. If no format provider is given, the format provider associated with the current thread is used.

To further illustrate, this sample invokes ToString as above, but displays the default, as well as the short and long date formats used in different cultures. This is done by simply looping thru the available cultures, and for each culture converts the given datetime instance using ToString, first with no format string parameter ( which by default uses the general format "G" ), then passing as format specifiers the characters ( "d" ) and ( "D" ), respectively, to specify short and long date format. In all these cases, the current CultureInfo object is passed as the format provider.

NOTE: Not all international fonts and code pages may be available or supported in your system, hence some characters or symbols may not render correctly.

 Show me 

Using similar logic, this sample invokes ToString as above, but displays a currency-formatted string instead for each of the given cultures.

 Show me 

More ...
Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note