1

我有一个计算 totalPrice 并将其返回到我的 totalPriceOutputLabel 的函数。我的问题是,我需要将输出格式化为例如“1,222”。我知道如何使用

ToString("C2")

但我不确定如何在我的函数调用中附加它。有任何想法吗?

Public Class tileLimitedForm

Private enteredLength, enteredWidth As Double
Private enteredPrice As Decimal

Public Function area(ByRef enteredLength As Double, ByRef enteredWidth As Double)
    area = Val(enteredLength) * Val(enteredWidth)
End Function

Public Function totalPrice(ByRef enteredLength As Double, ByRef enteredWidth As Double)
    totalPrice = Val(area(enteredLength, enteredWidth)) * Val(enteredPrice)
End Function

Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click

totalPriceOutputLabel.Text = totalPrice(area(enteredLength, enteredWidth),enteredPrice).ToString("C2")

End Sub
4

1 回答 1

0

像这样:

totalPriceOutputLabel.Text = _
    totalPrice(area(enteredLength, enteredWidth), enteredPrice).ToString("C2")

它假设totalPrice是一个Double或其他支持.ToString()带有格式参数的扩展的数字类型。

编辑

看到编辑后的问题后:

 Public Class tileLimitedForm

        Private enteredLength, enteredWidth As Double
        Private enteredPrice As Decimal

        Public Function area(ByVal enteredLength As Double, ByVal enteredWidth As Double) As Double
            area = enteredLength * enteredWidth
        End Function

        Public Function totalPrice(ByVal enteredLength As Double, ByvalenteredWidth As Double) As Double
            totalPrice = area(enteredLength, enteredWidth) * enteredPrice
        End Function

        Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click
            totalPriceOutputLabel.Text = totalPrice(area(enteredLength, enteredWidth), enteredPrice).ToString("C2")
        End Sub
    End Class

笔记:

  • 在这种情况下,您应该在函数中使用ByVal而不是ByRef
  • 您的函数当前返回 an ,Object因为您没有设置返回函数的类型(您已关闭 Option Strict)=> 我已添加As Double.
  • 不需要使用,Val因为参数已经是数字类型。
于 2013-09-25T04:18:24.717 回答