如何设置货币格式

时间:2013-10-12 19:51:44

标签: .net vb.net winforms

我想我已经找到了我对不同问题的答案,但程序会像我的任何格式一样使用以下代码。我需要格式化这一行:

Dim ProductString As String = txtProductID.Text.PadRight(12, " ") & "" & txtDescription.Text.PadRight(50, " ") & "" & txtQuantityAmount.Text.PadRight(7, " ") & "" & txtPriceAmount.Text.PadLeft(9, " ").ToString

具体来说,我需要txtPriceAmount.Text.PadLeft(9, " ").ToString接受货币格式(“C2”)。我做错了什么?

  Private Sub PurchaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As   System.EventArgs) Handles PurchaseToolStripMenuItem.Click

    'Test to determine if a product was found.
    If txtDescription.Text = String.Empty Then

        'Cannot purchase, product was not found
        MessageBox.Show("You must select a valid product before purchasing.", "Cannot Purchase", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        txtProductID.Focus()
        txtProductID.SelectAll()
    Else
        'Can purchase the product

        Dim ProductString As String = txtProductID.Text.PadRight(12, " ") & "" & txtDescription.Text.PadRight(50, " ") & "" & txtQuantityAmount.Text.PadRight(7, " ") & "" & txtPriceAmount.Text.PadLeft(9, " ").ToString
        lstPurchaseItems.Items.Add(ProductString)
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Accumulate the total value of this customer order
        'and display it to the output textbox
        TotalDueDecimal += (txtPriceAmount.Text.ToString * txtQuantityAmount.Text)
        txtTotalDueAmount.Text = TotalDueDecimal.ToString("C2")
        'TotalDueTextBox.Text = QuantityTextBox.Text * TotalDueDecimal.ToString("C2")

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


        'Accumulate total sales by product to an array
        Dim IndexInteger As Integer = cboProductIDLookup.SelectedIndex
        ProductSalesTotalDecimal(IndexInteger) += (txtPriceAmount.Text * txtQuantityAmount.Text)

        'Here you can clear the form of product info if you think
        'that is a good way to do the processing
        cboProductIDLookup.SelectedIndex = -1
        txtProductID.Clear()
        txtDescription.Clear()
        txtPriceAmount.Clear()
        txtQuantityAmount.Clear()
        txtProductID.Focus()
    End If
End Sub

1 个答案:

答案 0 :(得分:2)

您需要将txtPriceAmount.Text转换为数字,然后将其格式化为货币:

Double.Parse(txtPriceAmount.Text).ToString("C2").PadLeft(9, " ")

如果txtPriceAmount.Text不是数字,则会出错。