locked
Convert String to Decimal!!!!!!

    Question

  • Hi, I've been trying to add numbers in 2 textboxes (textbox1 and textbox2) and displaying answer into textbox2 but they are added as strings. So i put in this code

    Dim Number1 As String
    Dim Number2 As String
    Number1 = System.Convert.ToString(textbox1.Text)
    Number2= System.Convert.ToString(textbox2.Text)

    textbox2.Text = textbox1.Text + textbox2.Text

    but i keep getting the error that cannot convert string to decimal!!!!!!!!!!!!!!!!!! whats the way to add the numbers (which are strings at the moment) to each other?!!?


    Wednesday, February 20, 2008 6:10 PM

Answers

  • Hello.

     

    Try this:

     

    Dim Number1 As Double
    Dim Number2 As Double

    Dim Answer As Double

     

    Number1 = Val(textbox1.Text)
    Number2= Val(textbox2.Text)
    Answer = Number 1 + Number2

     

    Now you can do as you wish with the Answer variable.

    Wednesday, February 20, 2008 6:26 PM
  •  TritonGamer wrote:
    oh right, thts np. how come this doesnt work:

    Dim aNum As Decimal = Textbox1.Text

    Textbox2.Text = (Math.Round(aNum, 2))

    thanks

     

    Hi,

     

    You should test to see if the text value of a textbox is a value or use the TryParse method as suggested above.

     

    The IsNumeric function will test to see if it a number but apparently TryParse is better.

     

    TextBox.Text values are always a STRING so they need to be converted.

     

    Instead of CDEC you can also use

     

    aNum = Convert.ToDecimal(Textbox1.Text) 'if you know the STRING is a DECIMAL number otherwise you can get an

     

    'error if you do not use a TRY-CATCH-END TRY block or the TryParse method as suggested earlier.

     

     

     

     

     

    Regards,

     

    John

     

     

    Code Snippet

     

    Dim aNum As Decimal

     

    Try

     

    aNum = CDEC(TextBox1.Text) 'CDEC is short for Convert to DECimal

    Textbox2.Text = (Math.Round(aNum,2)).ToString

     

    Catch Ex As Exception

     

    End Try

     

     

     

    Wednesday, February 20, 2008 7:53 PM

All replies

  • Hello.

     

    Try this:

     

    Dim Number1 As Double
    Dim Number2 As Double

    Dim Answer As Double

     

    Number1 = Val(textbox1.Text)
    Number2= Val(textbox2.Text)
    Answer = Number 1 + Number2

     

    Now you can do as you wish with the Answer variable.

    Wednesday, February 20, 2008 6:26 PM
  • You might also try Convert.ToDecimal(stringValue), if you insist on declaring Number1 and Number 2 as strings. My apologies for not adding this to my previous post.

    Wednesday, February 20, 2008 6:31 PM
  • hey thanks alot buddy!!

    btw, wats Val mean?
    Wednesday, February 20, 2008 6:31 PM
  • The Val( ) function returns a numeric equivalent of a string. Keep in mind that it returns a Doube...so you may need to do conversions depending on your current situation. Glad I could help.

     

    Wednesday, February 20, 2008 6:33 PM
  • 1 more question, that textbox is showing numbers that are like to 5 or 6 decimal places (e.g. 123.23134)

    how can i make it auto round up to 2 decimal places?


    Wednesday, February 20, 2008 6:34 PM
  • Double data types have a precision of 7, if I'm not mistaken; while the Decimal data type has a precision of 2. Try this:

     

    Dim Number1 As Decimal

     

    Number1 = Convert.ToDecimal(Me.TextBox1.Text)

    Wednesday, February 20, 2008 6:41 PM
  • what about, like i got another textbox3, which changes every second continuously, and displays those numbers randomly, and those numbers are to 5dp, i put in that code, but wont work =(
    Wednesday, February 20, 2008 6:47 PM
  • Besides the methods VB offers there are methods in the dotnet framework, which you can use.

     

    All basic types offer a Parse and a TryParse method

    Dim MyDecimal As Decimal = Decimal.Parse(YourString)

    You will get an exception if your string contains something which can't be converted to a decimal.

    To prevent exceptions you can use

    Dim MyDecimal as Decimal

    if Decimal.TryParse(YourString,MyDecimal) Then

    ' Can't convert

    Else

    ' MyDecimal contains the converted value

    End If

    The same is true for the all the other basic types (Integer, Short, Date,...)

     

    To convert from basic types to string use:

    1. VB-Method

    Dim MyInteger As Integer = 12

    Dim MyString As String = Format(MyInteger,"f2")

    2. dotnet-framework

    Dim MyInteger As Integer = 12

    Dim MyString As String = MyInteger.ToString("n2")

     

    There are a lot of format specifier where you can tell the framework how you want to format (with thousand separator, how many numbers after the decimal point....)

     

    regards

    Philipp

    Wednesday, February 20, 2008 7:06 PM
  • You are right, and I apologize. I should've tested that. This will work.

     

    Dim aNum as Decimal = 15.123456789

    MessageBox.Show(Math.Round(aNum, 2))

     

    This should work for you.
    Wednesday, February 20, 2008 7:13 PM
  • oh right, thts np. how come this doesnt work:

    Dim aNum As Decimal = Textbox1.Text

    Textbox2.Text = (Math.Round(aNum, 2))

    thanks
    Wednesday, February 20, 2008 7:18 PM
  • A variable declared as Decimal only accepts decimal values. The property Text of the TextBox is of type String and only accepts Strings. VB is a strictly typed language: You must declare the type of your variable and you have to obey this type. Other languages like Python allow you to declare a variable without specifying a type. The type is determined by the system during runtime.

     

    That's the theory - in real life there is an option called 'strict' (see the Compile tab of your application properties).

    If strict is on (this is not the default but in my mind it should be) you can only assign values of the same type or of a type which implicitly convertible to the type of the variable (for example: you can assign an Integer to a Double variable).

    If strict is off (this is the default for being backward compatible to vb6) the system will help you a runtime. Its tries to convert and if it fails you get a runtime exception.

     

    If you add two numbers you will get something completly different to adding two string:

    2 + 2  --> 4

    "2" + "2"  --> "22"

    By the way, this is one reason to use &-operator to concatenate two strings

    "2" & 2  --> "22"   '  the number 2 is implicitly converted to a string

     

    regards

    Philipp

    Wednesday, February 20, 2008 7:42 PM
  •  TritonGamer wrote:
    oh right, thts np. how come this doesnt work:

    Dim aNum As Decimal = Textbox1.Text

    Textbox2.Text = (Math.Round(aNum, 2))

    thanks

     

    Hi,

     

    You should test to see if the text value of a textbox is a value or use the TryParse method as suggested above.

     

    The IsNumeric function will test to see if it a number but apparently TryParse is better.

     

    TextBox.Text values are always a STRING so they need to be converted.

     

    Instead of CDEC you can also use

     

    aNum = Convert.ToDecimal(Textbox1.Text) 'if you know the STRING is a DECIMAL number otherwise you can get an

     

    'error if you do not use a TRY-CATCH-END TRY block or the TryParse method as suggested earlier.

     

     

     

     

     

    Regards,

     

    John

     

     

    Code Snippet

     

    Dim aNum As Decimal

     

    Try

     

    aNum = CDEC(TextBox1.Text) 'CDEC is short for Convert to DECimal

    Textbox2.Text = (Math.Round(aNum,2)).ToString

     

    Catch Ex As Exception

     

    End Try

     

     

     

    Wednesday, February 20, 2008 7:53 PM
  • oh right i see now! thanks mate
    Wednesday, February 20, 2008 8:16 PM
  •  TritonGamer wrote:
    oh right i see now! thanks mate

     

    Hi,

     

    Please remember to Mark As Answer or / and As Helpful any other posts as appropriate.

     

    This is the easiest way.>>

     

    Try typing in just letters ( or letters the numbers or vice-versa ) first and see what happens.

     

     

    Regards,

     

    John

     

     

    Code Snippet

     

    Option Strict On

     

    Public Class Form1

     

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

     

     

    Dim aNum As Decimal

     

    'Try putting the text into the variable "aNum" >>

    Decimal.TryParse(TextBox1.Text, aNum)

     

    'Round up the result and convert it back to a STRING

    'then assign it to TextBox2.Text >>

    TextBox2.Text = Math.Round(aNum, 2).ToString

     

    End Sub

    End Class

     

     

    Wednesday, February 20, 2008 8:52 PM
  •  TritonGamer wrote:
    hey thanks alot buddy!!

    btw, wats Val mean?

     

    Hi,

     

    Val is short for VALUE and returns the VALUE in a STRING if it is a number.

     

    I would not recommend using it outside of a TRY-CATCH-END TRY block in this manner

     as you will get an exception error if you type in any letters by mistake.

     

     

     

     

    Regards,

     

    John

     

    Wednesday, February 20, 2008 8:56 PM
  • Input and output must be done with strings.  Convert to a number type for math processing. 

     

    You should use the TryParse method to convert the string to a Decimal type number.  The advantage of using TryParse is that you will get no error in case the textbox is blank or the user entered a string instead of a number.  Then use the ToString method to convert the result back to a string for display.  Here is the code.  You can output the result to the second textbox or use a messagebox.  I included both.

     

    Code Snippet

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim number1, number2, result As Decimal

    Decimal.TryParse(TextBox1.Text, number1)

    Decimal.TryParse(TextBox2.Text, number2)

    result = Math.Round(number1 + number2, 2)

    MessageBox.Show("Total is " & result.ToString)

    TextBox2.Text = result.ToString

    End Sub

     

     

     

    Wednesday, February 20, 2008 11:24 PM