none
Remove commas from numbers

    Question

  • I have a simple calculator that converts one number to another based on units. For example, convert a number in square inches to a number in square feet. There is a text box for entering the number to convert, two drop-downs to select the units and another text box to show the answer. Plus a button to start the conversion.

     

    The calculator works fine if the numbers are entered without commas. However, if commas are used, everything after the first comma is ignored.

     

    For example: 1,234 becomes 1 and the calculation is incorrect.

     

    How do I take out the comma(s)?

     

    Thanks,

    Spudguy

     

     

    Thursday, March 27, 2008 2:45 PM

Answers

  • it's the use of Val() that's doing it.

     

    basically your setting 'Start' to be 1 then trying to replace a none existing comma, the Val function has already broken your code.

     

    Try the following:

     

    Private Sub AreaCalcs2()

     

    Dim Start2 As String

    Start = Val(Txt_NumToConvert.Text.Replace(",", ""))

    Start2 = Start.ToString

     

    (Other code here, not necessary for example)

     

    Txt_Answer.Text = Start2.ToString

    End Sub

     

    That should fix it. that will tell the code to replace all comma and THEN use the val funtion to set the value. not the other wqay round as this is what's breaking your code.

     

    Thursday, March 27, 2008 4:04 PM

All replies

  • It's hard to say what's going on without seeing some code, but I'd guess your variable is a string. If so, try this

           Dim s As String = "1,234"
            s = s.Replace(",", "")

    Also, this may be of interest to you:

    http://msconline.maconstate.edu/tutorials/VBNET/VBNET01/vbnet01-08.aspx
    Thursday, March 27, 2008 3:14 PM
  • wherever in your code that you are referenceing the textbox.text value just use the Replace function of the string class.

     

    So lets say your input comes from Textbox1, then you could do this...

     

    Dim value As Double = TextBox1.Text.Replace(",", "")

     

    However, if you are converting the text to some numeric type (integer, double, decimal, etc.) then you should first be sure that the value is truly numeric, so I would do the following (assuming that double is the type you desire)...

     

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

       Dim value As Double

       If Double.TryParse(TextBox1.Text.Replace(",", ""), value) Then

           'Run your conversion with the Double variable "value".

           MessageBox.Show(value)

       Else

           'Reject the input. Here I use a messagebox.

           MessageBox.Show("Please enter a valid value to be converted.")

       End If

    End Sub

    Thursday, March 27, 2008 3:23 PM
  • Still no luck. What you suggested works as written, but the number entered could be anything.

     

    Here's my code:

     

    Code Snippet

    Private Sub AreaCalcs2()

     

    Dim Start2 As String

    Start = Val(Txt_NumToConvert.Text)

    Start2 = Start.ToString.Replace(",", "")

     

    (Other code here, not necessary for example)

     

    Txt_Answer.Text = Start2.ToString

    End Sub

     

     

    If I enter 1,234 the output to Txt.Answer is 1. If I enter 2,345, the output is 2.

     

    Thanks,

    Spudguy

    Thursday, March 27, 2008 3:42 PM
  • it's the use of Val() that's doing it.

     

    basically your setting 'Start' to be 1 then trying to replace a none existing comma, the Val function has already broken your code.

     

    Try the following:

     

    Private Sub AreaCalcs2()

     

    Dim Start2 As String

    Start = Val(Txt_NumToConvert.Text.Replace(",", ""))

    Start2 = Start.ToString

     

    (Other code here, not necessary for example)

     

    Txt_Answer.Text = Start2.ToString

    End Sub

     

    That should fix it. that will tell the code to replace all comma and THEN use the val funtion to set the value. not the other wqay round as this is what's breaking your code.

     

    Thursday, March 27, 2008 4:04 PM
  • Perfect! So simple when you see it.

     

    Thanks,

    Spudguy

     

    Thursday, March 27, 2008 4:41 PM
  • I have to ask if using the Val function is really serving your efforts well here.  By using Val you are essentially second-guessing the user's input by filtering that input through the semi-strict rules that Val employs.  In other words, if a user doesn't enter a true numeric value you are doing your best to derive some numeric value from that value.  It sounds like folly to me -- maybe not so much in a simple conversion program -- but for most cases like this a true validation of the whole input value would avoid the possibility of your misinterpreting the intended value.  Sure, removing commas is a start, but if you did a true validation of the input you would not need to use Val at all -- you should be able to get a real result based on one of the value types' TryParse functions or a Convert function.

     

    So let's say the user is fat-fingered and pressed the comma key while going for the period key and enters 23,.45 instead of 23.45.  Using Val you would tun 23 through your conversion method and return a value that is obviously not right for what the user thinks was the entered value.  So it's left up to the user to catch the fact an extra comma was entered -- otherwise he might just use the return value unwittingly.  Yipes!

     

    Maybe it's just me but it seems wrong to actually follow through with a method call just because you can get a value rather than determining if the entire value is valid.

    Thursday, March 27, 2008 5:22 PM
  • use ABS() function for example string = "1,234"   string2= ABS(string)

     

    displays  1234

     

    If you want to for get something, write it down to remember,..  Edger allen poe.

    Tuesday, May 13, 2008 10:50 PM
  • What you REALLY need to do is make sure the user's input is correct, by using the KeyPress event of the textbox.  The following code will only allow a correct numeric value to be entered.  If you don't need a fractional value or a negative number, then leave out those lines:

     

    Code Snippet

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If Not Char.IsDigit(e.KeyChar) Then e.Handled = True

    If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace

    If e.KeyChar = "-" And TextBox1.SelectionStart = 0 Then e.Handled = False 'allow negative number

    If e.KeyChar = "." And TextBox1.Text.IndexOf(".") = -1 Then e.Handled = False 'allow single decimal point

    If e.KeyChar = Chr(13) Then TextBox2.Focus() 'Enter key moves to specified control, OR:

    If e.KeyChar = Chr(13) Then GetNextControl(TextBox1, True).Focus() 'Enter key moves to next control

    End Sub

     

     

    Tuesday, May 13, 2008 11:01 PM
  • Give this a try. I used similar code to take commas out of a currency value.

    Code Snippet

    Dim Temp as String = txtInput.Text
    Dim WorkingValue as Double

    While Temp.Contains(",")

    Temp = Temp.Remove(Temp.LastIndexOf("."), 1)

    End While

    WorkingValue = CDbl(Temp)


    What it does is loop while there comma's left in the string value and removes the one closest to the right then parse the value to a double.
    Wednesday, May 14, 2008 2:22 AM