Formatting
Using Specifiers
Introduction
Standard numeric format strings are used to format common numeric types. A
standard format
string takes the form Axx where A is a single alphabetic character
called the format specifier,
and xx is an optional integer called the precision specifier.
The format
specifier must be one of the built-in format characters. The precision specifier ranges from 0 to 99 and
controls the number of
significant digits or zeros to the right of a decimal. The format string cannot
contain white spaces.
The following table describes the
standard numeric format strings. Note that the result string produced by these format specifiers is
influenced by the settings in the Regional Options control panel. Computers using different settings
will generate different result strings.
Some Specifiers
|
|
|
|
|
C or c |
Currency |
The number is converted
to a string that represents a currency amount. The
conversion is controlled by the currency format information
of the NumberFormatInfo
object used to format the number. The precision specifier
indicates the desired number of decimal places. If the
precision specifier is omitted, the default currency
precision given by the NumberFormatInfo is used. |
|
E or e |
Scientific (exponential) |
The number is converted
to a string of the form "-d.ddd…E+ddd" or "-d.ddd…e+ddd",
where each 'd' indicates a digit (0-9). The string starts
with a minus sign if the number is negative. One digit
always precedes the decimal point. The precision specifier
indicates the desired number of digits after the decimal
point. If the precision specifier is omitted, a default of
six digits after the decimal point is used. The case of the
format specifier indicates whether to prefix the exponent
with an 'E' or an 'e'. The exponent always consists of a
plus or minus sign and a minimum of three digits. The
exponent is padded with zeros to meet this minimum, if
required. |
|
F or f |
Fixed-point |
The number is converted
to a string of the form "-ddd.ddd…" where each 'd' indicates
a digit (0-9). The string starts with a minus sign if the
number is negative. The precision specifier indicates the
desired number of decimal places. If the precision specifier
is omitted, the default numeric precision given by the
NumberFormatInfo is used. |
|
N or n |
Number |
The number is converted
to a string of the form "-d,ddd,ddd.ddd…", where each 'd'
indicates a digit (0-9). The string starts with a minus sign
if the number is negative. Thousand separators are inserted
between each group of three digits to the left of the
decimal point. The precision specifier indicates the desired
number of decimal places. If the precision specifier is
omitted, the default numeric precision given by the
NumberFormatInfo is used. |
Example Code
Imports
System
Module
Module1
Sub Main()
Dim
MyDouble As
Double
= 123456789
Console.WriteLine("The
original number: " &
MyDouble)
Console.WriteLine("Original
number as Currency: " & MyDouble.ToString("C"))
Console.WriteLine("Original
number as Scientific: " & MyDouble.ToString("E"))
Console.WriteLine("Original
number as Number: " & MyDouble.ToString("N"))
Console.WriteLine("Original
number as Fixed Point: " & MyDouble.ToString("F"))
End Sub
End Module

Using Type Conversions
Syntax
|
|
Meaning
- Convert expression to Data Type Listed
|
|
CBool(expression)
CChar(expression)
CDate(expression)
CDbl(expression)
CDec(expression)
CInt(expression)
CSng(expression)
CStr(expression)
|
Boolean
Character
Date
Double
Decimal
Integer
Single
String |
Example Code
Module
Module1
Sub Main()
Dim
number1 As
Integer
= 14
Dim
number2 As
Double
= 15.36
Dim number3 as Double = 15.75
Console.WriteLine("number1
is: " & number1)
Console.WriteLine("number2
is: " & number2)
Console.WriteLine("number1
converted to single is: " &
CSng(number1))
Console.WriteLine("number2
converted to integer is: " &
CInt(number2))
Console.WriteLine("number3 converted to integer is: " &
CInt(number3))
End
Sub
End
Module

Using Math.Round
Module
Module1
Sub Main()
Dim firstNumber
As Double
= 3.1416
Dim secondNumber
As Double
= 3.1476
Dim thirdNumber
As Double
= Math.Round(firstNumber, 2)
Dim fourthNumber
As Double
= Math.Round(secondNumber, 3)
Console.WriteLine("firstNumber is: "
& firstNumber)
Console.WriteLine("secondNumber is: "
& secondNumber)
Console.WriteLine("firstNumber rounded
to 2 decimal places is: " & thirdNumber)
Console.WriteLine("secondNumber rounded
to 3 decimal places is: " & fourthNumber)
End Sub
End
Module
