vb.net FormatNumber用法

Title Use the FormatNumber function in Visual Basic .NET
Description This example shows how to use the FormatNumber function in Visual Basic .NET.
Keywords FormatNumber,format number,VB.NET
Categories Strings,VB.NET
The FormatNumber function returns a formatted string representation for a number. The Syntax is:
    FormatNumber(expression _
        [,digits_after_decimal] _
        [,include_leading_zero] _
        [,use_parens_if_negative] _
        [,groups_digits] )

Where:

expression
The numeric expression to format
digits_after_decimal
The number of digits to display after the decimal point
include_leading_zero
If the number is less than 1 and greater than -1,determines whether the number should have a leading 0 before the decimal point.
use_parens_if_negative
Determines whether negative numbers are surrounded with parentheses instead of using a minus sign.
groups_digits
Determines whether digits to the left of the decimal point are grouped with thousands separators (commas in the United States).

Examples:

Expression Result
FormatNumber(1.23456,2) 1.23
FormatNumber(0.123456,2,TriState.False) .12
FormatNumber(0.123456,TriState.True) 0.12
FormatNumber(-12345.12,TriState.False) -12,345.12
FormatNumber(-12345.12,TriState.True) (12,345.12)
FormatNumber(-12345.12,TriState.True,TriState.False) (12345.12)

This example uses the following code to display these examples in a TextBox.

Private Sub Form1_Load(ByVal sender As System.Object,ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim txt As String
    Dim x As Single

    x = 1.23456
    txt &= "FormatNumber(" & x.ToString() & ",2) = " & _
        FormatNumber(x,2) & vbCrLf
    x = 0.123456
    txt &= "FormatNumber(" & x.ToString() & "," & _
        "TriState.False) = " & FormatNumber(x,_
        TriState.False) & vbCrLf
    txt &= "FormatNumber(" & x.ToString() & "," & _
        "TriState.True) = " & FormatNumber(x,_
        TriState.True) & vbCrLf
    x = -12345.12345
    txt &= "FormatNumber(" & x.ToString() & ",_
        TriState.True) & vbCrLf
    txt &= "FormatNumber(" & x.ToString() & "," & _
        "TriState.True,TriState.False) = " & _
        FormatNumber(x,TriState.False) _
        & vbCrLf

    txtResult.Text = txt
    txtResult.Select(0,0)
End Sub

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...