货币格式编号 VB.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9265389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Money Format Number VB.NET
提问by John Nu?ez
I'm trying to convert a mathematical result of money format example:
我正在尝试转换货币格式示例的数学结果:
Dim num1 As Integer = 2000
Dim num2 As Integer = 500
msgbox(cDbl(num1 + num2))
It only returns 2500, which I need to return my 2,500.00 if anyone has any idea how I would be very helpful thanks.
它只返回 2500,如果有人知道我将如何提供帮助,我需要返回我的 2,500.00,谢谢。
回答by Dennis Traub
First, you should use Decimal
instead of Double
when handling monetary values. Double
has some rounding issues.
首先,您应该在处理货币值时使用Decimal
而不是Double
。Double
有一些舍入问题。
Second, you can use string formatting:
其次,您可以使用字符串格式:
Dim num1 As Integer = 2000
Dim num2 As Integer = 500
Diml value As Decimal = CDec(num1 + num2)
Dim formattedValue As String = String.Format("{0:n}", value)
msgbox(formattedValue)
回答by Rowland Shaw
Your MsgBox
shows you the value, but it hasn't formatted it, as you haven't asked it to.
您MsgBox
向您显示了该值,但它没有格式化它,因为您没有要求它。
If you went a little further and formatted the result as a string, you'll get the format you desire, e.g:
如果您更进一步并将结果格式化为 string,您将获得所需的格式,例如:
Dim num1 As Double = 2000
Dim num2 As Double = 500
Dim sum As Double = num1 + num2
MsgBox(sum.ToString("0.00")) ' Adjust format string to suit
回答by Gent
Standard Numeric Format String
is a great resource for general number formatting, the top one being currency (this takes into account culture differences)
是通用数字格式的重要资源,最重要的是货币(这考虑到了文化差异)
"C" or "c" for Currency
“C”或“c”表示货币
- Supported by: All numeric types.
- Precision specifier: Number of decimal digits.
- Default precision specifier: Defined by System.Globalization.NumberFormatInfo.
- 受支持:所有数字类型。
- 精度说明符:十进制位数。
- 默认精度说明符:由 System.Globalization.NumberFormatInfo 定义。
More information: The Currency ("C") Format Specifier.
更多信息:货币(“C”)格式说明符。
- 123.456 ("C", en-US) -> $123.46
- 123.456 ("C", fr-FR) -> 123,46
- 123.456 ("C", ja-JP) -> ¥123
- -123.456 ("C3", en-US) -> ($123.456)
- -123.456 ("C3", fr-FR) -> -123,456
- -123.456 ("C3", ja-JP) -> -¥123.456
- 123.456(“C”,美国)-> 123.46 美元
- 123.456(“C”,FR-FR)-> 123,46
- 123.456 ("C", ja-JP) -> ¥123
- -123.456(“C3”,美国)->(123.456 美元)
- -123.456(“C3”,FR-FR)-> -123,456
- -123.456 ("C3", ja-JP) -> -¥123.456
回答by Jon
If you want the format to be currency, either of these will work:
如果您希望格式为货币,则以下任一方法均可使用:
Dim num1 As Integer = 2000
Dim num2 As Integer = 500
MsgBox(String.Format("{0:C2}", num1 + num2))
Or
或者
Dim num1 As Integer = 2000
Dim num2 As Integer = 500
Dim sum As Integer = num1 + num2
MsgBox(sum.ToString("C2"))
回答by Umit SARIDOGAN
formatcurrency to double
格式货币翻倍
value = 1500,20 TL
ctype(value, double)
return 1500,20
double to formatcurrency
双重格式货币
sample
样本
value = 1500,1995
formatcurrency(value,2)
return = 1500,20 TL moneysembol (TL , $ ,vs..)
回答by Robert mbulanga
Dim num1 As Decimal = 20
Dim num2 As Decimal = 34
Dim ans As Decimal
ans = num1 + num2
MsgBox(ans.ToString("c"))
回答by Isuru
You normally don't use Integer
as the Datatype for currency values. Use Double
instead.
您通常不用Integer
作货币值的数据类型。使用Double
来代替。
I'm sure there are many ways to format the output string as a currency value.
我确信有很多方法可以将输出字符串格式化为货币值。
One if the methods I know has already been explained by Rowland Shaw. So I'll skip to the other one. It is the built-in function called FormatCurrency
. It will output the string as a currency value pluswith the currency symbol defined in your system.
如果我知道的方法已经由 Rowland Shaw 解释过。所以我会跳到另一个。它是名为 的内置函数FormatCurrency
。它会将字符串输出为货币值加上系统中定义的货币符号。
Dim num1 As Double = 2000
Dim num2 As Double = 500
Dim ans As Double = num1 + num2
MessageBox.Show(FormatCurrency(ans))
More details on FormatCurrency
, look here.
更多详情FormatCurrency
,请看这里。
回答by JSR
Take a look at the String.Format documentation, you will find what you need there.
看看 String.Format 文档,你会在那里找到你需要的东西。