Results 1 to 4 of 4

Thread: How do I get the amount to show as currency

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    11

    How do I get the amount to show as currency

    For some reason my string, amount, is not formatting as currency. Why? It's near the bottom of the code.
    Code:
    Option Strict On
    Public Class Form1
    
    
        Private Sub ClearBtn_Click(sender As Object, e As EventArgs) Handles ClearBtn.Click
            AmountDue.Text = ""
            HoursInput.Clear()
            RadioButtonA.Checked = False
            RadioButtonB.Checked = False
            RadioButtonC.Checked = False
            NPbox.Checked = False
    
        End Sub
    
        Private Sub ExitBtn_Click(sender As Object, e As EventArgs) Handles ExitBtn.Click
            End
        End Sub
    
        Private Sub CalcBtn_Click(sender As Object, e As EventArgs) Handles CalcBtn.Click
            Dim input As Double = CDbl(HoursInput.Text)
            If input < 0 Or input > 744 Then
                ErrorBox()
            Else
                Confirmation()
            End If
    
        End Sub
    
        Private Sub Confirmation()
            Dim box As String
    
            box = CStr(MessageBox.Show("Are you sure you want to submit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            If CDbl(box) = Windows.Forms.DialogResult.Yes Then
                Calculate()
            End If
        End Sub
        Private Sub Calculate()
            Dim hours, chosen, additional, discount As Double
            Dim amount As String
            hours = CDbl(HoursInput.Text)
            If RadioButtonA.Checked = True Then
                chosen = 9.95
                If hours > 200 Then
                    additional = (hours - 200) * 2
                Else
                    additional = 0
                End If
            ElseIf RadioButtonB.Checked = True Then
                chosen = 14.95
                If hours > 400 Then
                    additional = (hours - 400) * 1
                Else
                    additional = 0
                End If
            ElseIf RadioButtonC.Checked = True Then
                chosen = 19.95
            End If
            If NPbox.Checked = True Then
                discount = 0.8
            Else
                discount = 1
            End If
            amount = CStr((chosen + additional) * discount)
            AmountDue.Text = String.Format("{0:c}", amount)
    
    
        End Sub
        Private Sub ErrorBox()
            Dim ERbox As String = CStr(MessageBox.Show("Please enter a number between 0 and 744", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error))
            If CDbl(ERbox) = Windows.Forms.DialogResult.OK Then
                HoursInput.Text = ""
            End If
        End Sub
    End Class

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: How do I get the amount to show as currency

    You have your amount variable declared as a String, it should be a Double or even better a Decimal. Take a look at this example:
    Code:
    Dim strAmount As String = "1.5"
    Dim decAmount As Decimal = CDec(1.5)
    
    Console.WriteLine(String.Format("String: {0:c}", strAmount))
    Console.WriteLine(String.Format("Decimal: {0:c}", decAmount))
    Console.ReadLine()
    By the way, I prefer ToString over Format for currency:
    Code:
    Dim amount As Decimal = CDec(1.5)
    
    Console.WriteLine(amount.ToString("c"))
    Console.ReadLine()
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    11

    Re: How do I get the amount to show as currency

    Awesome, thank you. I'm very new, so for curiosity, why didn't the variable format when it was declared as a string?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: How do I get the amount to show as currency

    That's just how it behaves. The data type must be numeric(decimal, double, integer ect.) in order to format properly. Technically you could've converted the string value to a decimal, but that would be unnecessary and inefficient. Besides, the variable should have never been a string in the first place.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width