vb.net FormatNumber用法

 
TitleUse the FormatNumber function in Visual Basic .NET
DescriptionThis example shows how to use the FormatNumber function in Visual Basic .NET.
KeywordsFormatNumber, format number, VB.NET
CategoriesStrings, 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:

ExpressionResult
FormatNumber(1.23456, 2)1.23
FormatNumber(0.123456, 2, TriState.False).12
FormatNumber(0.123456, 2, 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() & ", 2, " & _
        "TriState.False) = " & FormatNumber(x, 2, _
        TriState.False) & vbCrLf
    txt &= "FormatNumber(" & x.ToString() & ", 2, " & _
        "TriState.True) = " & FormatNumber(x, 2, _
        TriState.True) & vbCrLf
    x = -12345.12345
    txt &= "FormatNumber(" & x.ToString() & ", , " & _
        "TriState.False) = " & FormatNumber(x, 2, , _
        TriState.False) & vbCrLf
    txt &= "FormatNumber(" & x.ToString() & ", , " & _
        "TriState.True) = " & FormatNumber(x, 2, , _
        TriState.True) & vbCrLf
    txt &= "FormatNumber(" & x.ToString() & ", , " & _
        "TriState.True, TriState.False) = " & _
        FormatNumber(x, 2, , TriState.True, TriState.False) _
        & vbCrLf

    txtResult.Text = txt
    txtResult.Select(0, 0)
End Sub
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值