货币格式数字 VB.NET

发布于 2025-01-04 18:27:34 字数 185 浏览 4 评论 0原文

我正在尝试转换货币格式示例的数学结果:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

小鸟爱天空丶 2025-01-11 18:27:34

首先,在处理货币值时,您应该使用 Decimal 而不是 DoubleDouble 有一些舍入问题。

其次,您可以使用字符串格式

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)

First, you should use Decimal instead of Double when handling monetary values. Double has some rounding issues.

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)
无可置疑 2025-01-11 18:27:34

您的 MsgBox 向您显示了该值,但它尚未格式化它,因为您没有要求它这样做。

如果您进一步将结果格式化为字符串,您将获得所需的格式,例如:

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

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:

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
漫雪独思 2025-01-11 18:27:34

标准数字格式字符串

是通用数字格式的一个很好的资源,顶部一种是货币(考虑到文化差异)

“C”或“c”代表货币

  • 支持者:所有数字类型。
  • 精度说明符:小数位数。
  • 默认精度说明符:由 System.Globalization.NumberFormatInfo 定义。

更多信息:货币(“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

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

  • Supported by: All numeric types.
  • Precision specifier: Number of decimal digits.
  • Default precision specifier: Defined by System.Globalization.NumberFormatInfo.

More information: The Currency ("C") Format Specifier.

  • 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
暖心男生 2025-01-11 18:27:34

如果您希望格式为货币,则以下任一方法都可以:

    Dim num1 As Integer = 2000
    Dim num2 As Integer = 500
    MsgBox(String.Format("{0:C2}", num1 + num2))

或者

    Dim num1 As Integer = 2000
    Dim num2 As Integer = 500
    Dim sum As Integer = num1 + num2

    MsgBox(sum.ToString("C2"))

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"))
埋葬我深情 2025-01-11 18:27:34

将货币格式化为双精度

value = 1500,20 TL

ctype(value, double)

return 1500,20

将双精度格式化为货币

示例

value = 1500,1995

formatcurrency(value,2)

return = 1500,20 TL moneysembol (TL , $ ,vs..)

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..)
蓝眸 2025-01-11 18:27:34
Dim num1 As Decimal = 20
Dim num2 As Decimal = 34
Dim ans As Decimal

ans = num1 + num2
MsgBox(ans.ToString("c"))
Dim num1 As Decimal = 20
Dim num2 As Decimal = 34
Dim ans As Decimal

ans = num1 + num2
MsgBox(ans.ToString("c"))
遗忘曾经 2025-01-11 18:27:34

查看 String.Format 文档,您会在那里找到您需要的内容。

Take a look at the String.Format documentation, you will find what you need there.

筑梦 2025-01-11 18:27:34

您通常不使用Integer 作为货币值的数据类型。请改用Double

我确信有很多方法可以将输出字符串格式化为货币值。

一种是我所知道的方法已经由罗兰·肖解释过。所以我会跳到另一篇。它是名为 FormatCurrency 的内置函数。它将输出字符串作为货币值加上以及系统中定义的货币符号。

Dim num1 As Double = 2000
Dim num2 As Double = 500
Dim ans As Double = num1 + num2

MessageBox.Show(FormatCurrency(ans))

有关 FormatCurrency 的更多详细信息,请参阅

You normally don't use Integer as the Datatype for currency values. Use Double 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文