货币格式数字 VB.NET
我正在尝试转换货币格式示例的数学结果:
Dim num1 As Integer = 2000
Dim num2 As Integer = 500
msgbox(cDbl(num1 + num2))
它只返回 2500,我需要返回我的 2,500.00 如果有人知道我会非常有帮助,谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
首先,在处理货币值时,您应该使用
Decimal
而不是Double
。Double
有一些舍入问题。其次,您可以使用字符串格式:
First, you should use
Decimal
instead ofDouble
when handling monetary values.Double
has some rounding issues.Second, you can use string formatting:
您的
MsgBox
向您显示了该值,但它尚未格式化它,因为您没有要求它这样做。如果您进一步将结果格式化为字符串,您将获得所需的格式,例如:
Your
MsgBox
shows you the value, but it hasn't formatted it, as you haven't asked it to.If you went a little further and formatted the result as a string, you'll get the format you desire, e.g:
标准数字格式字符串
是通用数字格式的一个很好的资源,顶部一种是货币(考虑到文化差异)
“C”或“c”代表货币
更多信息:货币(“C”)格式说明符。
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
More information: The Currency ("C") Format Specifier.
如果您希望格式为货币,则以下任一方法都可以:
或者
If you want the format to be currency, either of these will work:
Or
将货币格式化为双精度
将双精度格式化为货币
示例
formatcurrency to double
double to formatcurrency
sample
查看 String.Format 文档,您会在那里找到您需要的内容。
Take a look at the String.Format documentation, you will find what you need there.
您通常不使用
Integer
作为货币值的数据类型。请改用Double
。我确信有很多方法可以将输出字符串格式化为货币值。
一种是我所知道的方法已经由罗兰·肖解释过。所以我会跳到另一篇。它是名为
FormatCurrency
的内置函数。它将输出字符串作为货币值加上以及系统中定义的货币符号。有关
FormatCurrency
的更多详细信息,请参阅You normally don't use
Integer
as the Datatype for currency values. UseDouble
instead.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 plus with the currency symbol defined in your system.More details on
FormatCurrency
, look here.