Formatting Numbers in ASP.Net

Formatting is the process of converting an instance of a class, structure, or enumeration value to its string representation. Anytime you display a DateTime or Numeric variables in an ASP.NET page, you are formatting that variable from its native type into some sort of string representation. How a DateTime or Numeric variable is formatted depends on the culture settings and the format string. Because dates and numeric values are formatted differently across cultures, the .NET Framework bases its formatting on the specified culture settings. By default, the formatting routines use the culture settings defined on the web server, but you can indicate that a particular culture be used anytime you format. In addition to the culture settings, formatting is also affected by a format string, which spells out the formatting details to apply.
There are a couple of different ways to format a DateTime or numeric variable. The first way involves using the ToString method, which has a number of overrides. Formatting can also be performed using the String.Format method. The String.Format method accepts as input a format string that contains a number of placeholders, followed by the values to plug into those placeholders. Placeholders take the form {0}, {1}, and so on, and may optional include a format string like so: {0:formatString}.The following code snippet shows using the String.Format method.

Dim CustName As String = “Narendra”
Dim subtotal as Decimal = 245.13
Dim output As String = String.Format(“{0}, your order subtotal is {1:c}.”, CustName, subtotal)

Standard Numeric Format String
Standard numeric format strings are used to format common numeric types. A standard numeric format string takes the form Axx, where A is an alphabetic character called the format specifier, and xx is an optional integer called the precision specifier. The precision specifier ranges from 0 to 99 and affects the number of digits in the result. Any numeric format string that contains more than one alphabetic character, including white space, is interpreted as a custom numeric format string.

Format Name Code snippet Output
1. Currency(“C” or “c”)
Result : A currency value.
Supported by : All numeric types.
Precision Specifier : Number of decimal digits.
123.ToString(“C”, New CultureInfo(“en-US”))
3.14159.ToString(“C2”, New CultureInfo(“en-US”))
$123.00
$3.14
2. Decimal(“D” or “d”)
Result : Integer digits with optional negative sign.
Supported by : Integral types only.
Precision Specifier : Minimum number of digits.
1234.ToString(“D”)
1234.ToString(“D6”)
1234
001234
3. Exponential(“E” or “e”)
Result : Exponential notation.
Supported by : All numeric types.
Precision Specifier : Number of decimal digits.
1052.0329112756.ToString(“E”, New CultureInfo(“en-US”))
1052.0329112756.ToString(“E2”, New CultureInfo(“en-US”))
1.052033E+003
1.05E+003
4. Fixed-point(“F” or “f”)
Result : Integral and decimal digits with optional negative sign.
Supported by : All numeric types.
Precision Specifier : Number of decimal digits.
1234.567.ToString(“F”, New CultureInfo(“en-US”))
1234.567.ToString(“F4”, New CultureInfo(“en-US”))
1234.57
1234.5670
5. General(“G” or “g”)
Result : The most compact of either fixed-point or scientific notation.
Supported by : All numeric types.
Precision Specifier : Number of significant digits.
123.4546.ToString(“G4”, New CultureInfo(“en-US”)) 123.5
6. Number(“N” or “n”)
Result : Integral and decimal digits, group separators, and a decimal separator with optional negative sign.
Supported by : All numeric types.
Precision Specifier : Desired number of decimal places.
1234.56712.ToString(“N”, New CultureInfo(“en-US”))
1234.56712.ToString(“N3”, New CultureInfo(“en-US”))
1,234.57
1,234.567
7. Percent(“P” or “p”)
Result : Number multiplied by 100 and displayed with a percent symbol.
Supported by : All numeric types.
Precision Specifier : Desired number of decimal places.
1.ToString(“P”, New CultureInfo(“en-US”))
12.56712.ToString(“P2”, New CultureInfo(“en-US”))
100.00 %
1,256.71 %
8. Round-trip(“R” or “r”)
Result : A string that can round-trip to an identical number.The round-trip (“R”) format specifier guarantees that a numeric value that is converted to a string will be parsed back into the same numeric value.
Supported by : Single, Double, and BigInteger.
Precision Specifier : Ignored.
3.14159.ToString(“R”, New CultureInfo(“en-US”)) 3.14159
9. Hexadecimal(“X” or “x”)
Result : A hexadecimal string.
Supported by : Integral types only.
Precision Specifier : Number of digits in the result string.
255.ToString(“X”, New CultureInfo(“en-US”))
255.ToString(“X4”, New CultureInfo(“en-US”))
FF
00FF

Custom Numeric Format String
If the standard numeric format specifiers do not provide the type of formatting you require, you can use custom format strings to further enhance string output. A standard format string consists of a single alphabetic character optionally followed by a sequence of digits that form a value between 0 and 99; all other format strings are custom format strings.
The following table shows the characters you can use to create custom numeric format strings and their definitions.

Format Name Code snippet Output
1. Zero placeholder(0)
Description : If the value being formatted has a digit in the position where the
‘0’ appears in the format string, then that digit is copied to the result string.
The position of the leftmost ‘0’ before the decimal point and the rightmost ‘0’
after the decimal point determines the range of digits that are always present in
the result string.
3.14.ToString(“00.0000”, New CultureInfo(“en-US”))
123.45678.ToString(“00.0000”, New CultureInfo(“en-US”))
03.1400
123.4568
2. Digit placeholder(#)
Description : If the value being formatted has a digit in the position where the
‘#’ appears in the format string, then that digit is copied to the result string.
Otherwise, nothing is stored in that position in the result string.
3.14.ToString(“#0.000#”, New CultureInfo(“en-US”))
123.45678.ToString(“#0.000#”, New CultureInfo(“en-US”))
3.140
123.4568
3. Decimal point(.)
Description : The first ‘.’ character in the format string determines the location
of the decimal separator in the formatted value; any additional ‘.’ characters are
ignored.
123.446.ToString(“#####.#”, New CultureInfo(“en-US”))
12345.6.ToString(“####.##”, New CultureInfo(“en-US”))
123.4
12345.6
4. Thousand separator(,)
Description : If the format string contains a ‘,’ character between two placeholders
(0 or #), then the output will have thousand separators inserted between each group
of three digits to the left of the decimal separator.
123456.ToString(“#,000.00”, New CultureInfo(“en-US”)) 123,456.0
5. Number scaling(,)
Description : If the format string contains one or more ‘,’ characters immediately
to the left of the decimal point, then the number will be divided by the number
of ‘,’ characters multiplied by 1000 before it is formatted.
2000000.ToString(“0,,”, New CultureInfo(“en-US”))
2000000.ToString(“0,”, New CultureInfo(“en-US”))
2
2000
6. Percentage placeholder(%)
Description : The presence of a ‘%’ character in a format string causes a number
to be multiplied by 100 before it is formatted. The appropriate symbol is inserted
in the number itself at the location where the ‘%’ appears in the format string.
0.086.ToString(“#0.##%”, New CultureInfo(“en-US”))
0.086.ToString(“#0%.##”, New CultureInfo(“en-US”))
8.6%
8%.6
7. Scientific notation(E0,E+0,E-0,e0,e+0,e-0)
Description : If any of the strings “E”, “E+”, “E-“, “e”, “e+”, or “e-” are present
in the format string and are followed immediately by at least one ‘0’ character,
then the number is formatted using scientific notation with an ‘E’ or ‘e’ inserted
between the number and the exponent. The number of ‘0’ characters following the
scientific notation indicator determines the minimum number of digits to output
for the exponent. The “E+” and “e+” formats indicate that a sign character (plus
or minus) should always precede the exponent. The “E”, “E-“, “e”, or “e-” formats
indicate that a sign character should only precede negative exponents.
86000.ToString(“0.###E+0”, New CultureInfo(“en-US”))
86000.ToString(“0.###E+000”, New CultureInfo(“en-US”))
86000.ToString(“0.###E-000”, New CultureInfo(“en-US”))
8.6E+4
8.6E+004
8.6E004
8. Section separator(;)
Description : The ‘;’ character is used to separate sections for positive, negative,
and zero numbers in the format string.
1234.ToString(“##;(##)”, New CultureInfo(“en-US”))
-1234.ToString(“##;(##)”, New CultureInfo(“en-US”))
1234
(1234)