locked
Calculate numbers with comma and decimal RRS feed

  • Question

  • Hi everyone, i started understanding some calculation code but finding difficulty in some aspect. Quick Scenario: i have 3 textbox wherein if you type 1234567.5678 it will automatically display 1,234,567.57

    HERE is the code from 1 of the 3 textboxes namely txtAmountCash which has the same code for the rest of the 2 textboxes. (i got this code from the internet)

        Private Sub txtAmountCash_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtAmountCash.KeyPress
            Dim txtval As String = txtAmountCash.Text
            Dim decval As Decimal
            If Not Char.IsDigit(e.KeyChar) Then e.Handled = True 'no non-numeric characters allowed
            If e.KeyChar = Chr(8) Then e.Handled = False 'allow backspace
            If Not txtval.Contains(".") And e.KeyChar = "," Then e.Handled = False 'allow commas ONLY if no decimal points present
            If e.KeyChar = "." And txtAmountCash.Text.IndexOf(".") = -1 Then e.Handled = False 'allow single decimal point
            If txtval.Length >= 28 Then
                MessageBox.Show("Maximum number of digits limited to 28")
                If e.KeyChar = Chr(8) Then e.Handled = False Else e.Handled = True
                txtAmountCash.Text = txtval.Substring(0, 28)     'allow backspace to work
                txtAmountCash.SelectionStart = txtAmountCash.TextLength   'cursor moved to end of text
            End If
            If e.KeyChar = Chr(13) Then
                Decimal.TryParse(txtval, decval)
                If txtval.Contains(".") Then
                    txtval = decval.ToString("n")       'displays with 2 places after decimal point
                Else
                    txtval = Format(decval, "###,###") 'no decimal point
                End If
                txtAmountCash.Text = txtval
            End If
        End Sub

        Private Sub txtAmountCash_LostFocus(sender As Object, e As System.EventArgs) Handles txtAmountCash.LostFocus
            Dim txtval As String = txtAmountCash.Text
            Dim decval As Decimal
            If Decimal.TryParse(txtval, decval) Then
                If txtval.Contains(".") Then
                    txtval = decval.ToString("n")       'displays with 2 places after decimal point
                Else
                    txtval = Format(decval, "###,###") 'no decimal point
                End If
            End If
            txtAmountCash.Text = txtval
        End Sub


    If I input 1000000.567 to txtAmountCash, it automatically changes to 1,000,000.57

    If I input 2500000.678 to txtAmountCheck, it automatically changes to 2,500,000.68

    If I input 3000000.789 to txtAmountCredit, it automatically changes to 3,000,000.79

    HERE'S MY QUESTION: how can i add these 3 textboxes?

    It must show a result of 6,500,002.04

    Thursday, November 6, 2014 12:40 PM

Answers

  • Convert the text in the textboxes to Decimal variables and add them.

    Dim num1 as Decimal = CDec(TextBox1.Text)
    Dim num2 as Decimal = CDec(TextBox2.Text)
    Dim num3 as Decimal = CDec(TextBox3.Text)
    
    Dim total As Decimal = num1 + num2 + num3
    MessageBox.Show("Total is :" & total.ToString("N2"))


    • Edited by Blackwood Thursday, November 6, 2014 12:48 PM
    • Marked as answer by nadzson Thursday, November 6, 2014 1:06 PM
    Thursday, November 6, 2014 12:47 PM
  • You should take a look at using Decimal.TryParse.  One of the overloads allows commas and decimal points.

    Imports System.Globalization
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim value As String = "$1,097.63"
            Dim style As NumberStyles
            Dim culture As CultureInfo = CultureInfo.CurrentCulture
            Dim number As Decimal
    
            style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
    
            If Decimal.TryParse(value, style, culture, number) Then
                Debug.WriteLine("Converted '{0}' to {1}.", value, number)
            Else
                Debug.WriteLine("Unable to convert '{0}'.", value)
            End If
        End Sub
    End Class
    


    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer

    • Proposed as answer by KareninstructorMVP Thursday, November 6, 2014 1:03 PM
    • Marked as answer by nadzson Thursday, November 6, 2014 1:06 PM
    Thursday, November 6, 2014 12:50 PM
  • You should convert the text in the TextBoxes to decimals before you do the calculation:

            Dim sum As Decimal = Decimal.Parse(txtAmountCash.Text, System.Globalization.CultureInfo.InvariantCulture) + Decimal.Parse(txtAmountCheck.Text, System.Globalization.CultureInfo.InvariantCulture) + Decimal.Parse(txtAmountCredit.Text, System.Globalization.CultureInfo.InvariantCulture)

    Of course this assumes that the TextBoxes contain some text that can actually be converted into decimals like for example "1,000,000.57"

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by nadzson Thursday, November 6, 2014 1:06 PM
    Thursday, November 6, 2014 12:54 PM

All replies

  • Convert the text in the textboxes to Decimal variables and add them.

    Dim num1 as Decimal = CDec(TextBox1.Text)
    Dim num2 as Decimal = CDec(TextBox2.Text)
    Dim num3 as Decimal = CDec(TextBox3.Text)
    
    Dim total As Decimal = num1 + num2 + num3
    MessageBox.Show("Total is :" & total.ToString("N2"))


    • Edited by Blackwood Thursday, November 6, 2014 12:48 PM
    • Marked as answer by nadzson Thursday, November 6, 2014 1:06 PM
    Thursday, November 6, 2014 12:47 PM
  • You should take a look at using Decimal.TryParse.  One of the overloads allows commas and decimal points.

    Imports System.Globalization
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim value As String = "$1,097.63"
            Dim style As NumberStyles
            Dim culture As CultureInfo = CultureInfo.CurrentCulture
            Dim number As Decimal
    
            style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
    
            If Decimal.TryParse(value, style, culture, number) Then
                Debug.WriteLine("Converted '{0}' to {1}.", value, number)
            Else
                Debug.WriteLine("Unable to convert '{0}'.", value)
            End If
        End Sub
    End Class
    


    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer

    • Proposed as answer by KareninstructorMVP Thursday, November 6, 2014 1:03 PM
    • Marked as answer by nadzson Thursday, November 6, 2014 1:06 PM
    Thursday, November 6, 2014 12:50 PM
  • You should convert the text in the TextBoxes to decimals before you do the calculation:

            Dim sum As Decimal = Decimal.Parse(txtAmountCash.Text, System.Globalization.CultureInfo.InvariantCulture) + Decimal.Parse(txtAmountCheck.Text, System.Globalization.CultureInfo.InvariantCulture) + Decimal.Parse(txtAmountCredit.Text, System.Globalization.CultureInfo.InvariantCulture)

    Of course this assumes that the TextBoxes contain some text that can actually be converted into decimals like for example "1,000,000.57"

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by nadzson Thursday, November 6, 2014 1:06 PM
    Thursday, November 6, 2014 12:54 PM
  • thanks, i tried all of your suggestions and it worked. Thanks for your relies, appreciate much! 
    Thursday, November 6, 2014 1:09 PM