Вы находитесь на странице: 1из 2

11/16/2015 VB Helper: HowTo: Use the FormatCurrency function in Visual Basic .

NET

 
Use the FormatCurrency function in Visual
Home Title
Basic .NET
Search
This example shows how to use the
 
Description FormatCurrency function in Visual Basic
What's New
.NET.
Index
Books FormatCurrency, format currency,
Keywords
Links VB.NET
Q&A Categories Strings, VB.NET
Newsletter  
  Banners   The FormatCurrency function returns a formatted string
  representation for a number representing currency. It is
Feedback
very similar to FormatNumber except it adds the system's
Tip Jar currency symbol to the result. The syntax is:
 
C# Helper... FormatCurrency(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,
MSDN Visual determines whether the number should have a
Basic Community 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 (in the United States):


 
http://www.vb-helper.com/howto_net_format_currency.html 1/2
11/16/2015 VB Helper: HowTo: Use the FormatCurrency function in Visual Basic .NET

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

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 &= "FormatCurrency(" & x.ToString() & ", 3) = " & _
FormatCurrency(x, 2) & vbCrLf

x = 0.123456
txt &= "FormatCurrency(" & x.ToString() & ", 4, " & _
"TriState.false) = " & FormatCurrency(x, 2, _
TriState.False) & vbCrLf
txt &= "FormatCurrency(" & x.ToString() & ", 4, " & _
"TriState.True) = " & FormatCurrency(x, 2, _
TriState.True) & vbCrLf

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

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

Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
 http://www.vb-helper.com/howto_net_format_currency.html Updated 11/10/2010 15:06:26  

http://www.vb-helper.com/howto_net_format_currency.html 2/2

Вам также может понравиться