Convert string to decimal

  • 8 years ago

    how to you convert a string to a decimal?

  • 8 years ago
    just for converting string to decimal

       Dim strw As String = "1"
           Dim c As Decimal = CDec (strw)

  • 8 years ago
    When converting any numerical values, if you know that they are valid numbers, just use CDec, CInt, CDbl as VasanthKumar suggets.  Alternatively, you can use the members of the System.Convert class.  If you don't know for sure that the value will be a valid number, you should use the Double.TryParse method.  It allows you to specify what type of numerical formats are accepted and returns a boolean value indicating whether the conversion was successful.  If the function returns True, the converted value is contained in a Double argument that was passed by reference, which you can then convert to Integer, Decimal or whatever is needed.  This avoids any exceptions being thrown if the value is not a valid number.
  • 8 years ago

    i have recently try an database example from "Sam Teach Yourself Visual Basic.Net in 24". There is a particular statement i don't really understand.
    txtItemID.Text = _
             mdtTelescopes.Rows(mrowPosition)("ItemID").ToString
    Does the above statement converts whether in the "ItemID" property to a string?
    According to the example, the "ItemID" property is a text. But i modifed it to an integer. So likewise in the MS Access Databsase i did the changes.


    The Add record function is as below
    drNewRow("ItemID") = txtNewItemID.Text


    i try to convert the "ItemID" to an integer but the program runs to an exception it goes like this:


    An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll


    Additional information: System.FormatException: Input string was not in a correct format.
     at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
     at System.Int32.Parse(String s, NumberStyles style, IFormatProvider provider)
     at System.Convert.ToInt32(String value, IFormatProvider provider)
     at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
     at System.Convert.ToInt32(Object value)
     at System.Data.Common.Int32Storage.Set(Int32 record, Object value)
     at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <> in ItemID Column.  Expected type is Int32.


    I post this msg last wk but got no reply. Can u help?

  • 8 years ago

    Quote:
    i try to convert the "ItemID" to an integer but the program runs to an exception it goes like this:
     


    here instead of
    drNewRow("ItemID") = txtNewItemID.Text


    check



    if isNumeric(txtNewItemID.Text.trim)=true
    drNewRow("ItemID") =  cint(txtNewItemID.Text.trim)
     end if

  • 8 years ago

    Hi. your code works well. Thanks. Got another question 4 u. i have create a few textboxes that represents "Price" and "Distance". Now, in my MS Access, the "Price" property is set to 2 decimal places while the "Distance" is an integer. How to i validate the data so that the program checks that "Price has only 2 decimal places while "distance" is a whole number? If i type a price that contains 3 or more decimal places the data is still saved and sucessfully added to the database. But when i try to delete that particular errorous record, an exception pops up. It goes like this:
    An unhandled exception of type 'System.Data.DBConcurrenyException' occurred in system.data.dll
    Additional information: Conncurrency violation: the DeleteCommand affected 0 records.
    So i closed the whole progam and start up again. this time the errorous record is still there but the "Price" has been changed to 2 decimal places and it can be deleted this time. How to i go abt preventing this excecption from showing?

  • 8 years ago

    You could use NumericUpDown controls.  They allow you to set maximum and minimum values and the number of decimal places.  They also handle validation for you as they will not allow non-numerical input.  They do have a few quirks, though, so if you haven't used them before I suggest reading about them in the help and experimenting a bit.  The Value property of a NumericUpDowm control is a Decimal object.

  • 8 years ago

    Quote:
    [1]Posted by net11573 on 2 Jul 2005 03:21 AM[/1]
    Hi. your code works well. Thanks. Got another question 4 u. i have create a few textboxes that represents "Price" and "Distance". Now, in my MS Access, the "Price" property is set to 2 decimal places while the "Distance" is an integer. How to i validate the data so that the program checks that "Price has only 2 decimal places while "distance" is a whole number? If i type a price that contains 3 or more decimal places the data is still saved and sucessfully added to the database. But when i try to delete that particular errorous record, an exception pops up. It goes like this:
    An unhandled exception of type 'System.Data.DBConcurrenyException' occurred in system.data.dll
    Additional information: Conncurrency violation: the DeleteCommand affected 0 records.
    So i closed the whole progam and start up again. this time the errorous record is still there but the "Price" has been changed to 2 decimal places and it can be deleted this time. How to i go abt preventing this excecption from showing?



    ACTUALLY FOR THESE Cases like allowing only upto 2 decimal palces,checking if its a percentage like that u can use Regular Expression


    do this on The validating event of Price textbox
    write

    Private regex3 As New Regex("^((\d){1,5}.(\d){1,2})$")

    with this above regex the problem of aceepting alphabets,invalidcharacters etc are taken care.


    Code:

       Private Sub (Price)TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
           If Not regex3.IsMatch((Price)TextBox1.Text) = True Then
               MsgBox("invalid")
             e.Cancel = True
           End If

    for whole number
    how many digit u need


    ("^((\d){1,5})$") this regex takes only whole number maximum of 5



    here price column allows allow maximum of 5 digits before decimal and 2 after decimal.


    u can modify the regex according to u r requirement


       End Sub

  • 8 years ago

    i try add the "Private regex3 As New Regex("^((\d){1,5}.(\d){1,2})$")" but got an error  Type 'Regex' is not defined

  • 8 years ago

    The Regex class is in the System.Text.RegularExpressions namespace.  If ever you get this error simply do a help search on the class name to find out what namespace the class is in.  Then you either qualify the type:

    Code:
    Private myRegex As System.Text.RegularExpressions.Regex
    or you import the namespace:Code:
    Import System.Text.RegularExpressions


    Private myRegex As Regex

  • 8 years ago
    i do not quite understand the expression  ("^((\d){1,5}\.(\d){1,2})$")  Can u explain a little?
  • 8 years ago
    Regular Expression syntax is a course in itself.  It is very powerful but very confusing to the beginner.  I don't claim to know more that a little of it myself.  The expression VasanthKumar has supplied would require that you have at least one but not more than five digit characters, followed by a decimal point, followed by at least one but not more that two digit characters.  I'd suggest you do a bit of Googling for regular expressions.
  • 8 years ago
    Quote:
    [1]Posted by net11573 on 3 Jul 2005 03:07 AM[/1]
    i do not quite understand the expression  ("^((\d){1,5}\.(\d){1,2})$")  Can u explain a little?


    \d represents(0,1,2,3,4,5,6,7,8,9)
    \d(1,5) means atleast one digit must be entered or maximum of 5 digits.
    \. means dot must be entered
    \d(1,2) means atleast one digit must be entered or max of 2

    here \d(1,5) is followed by  \.  followed by \d(1,2)
    ie
    these are valid
    12345.12
    1234.12
    123.12
    12.12
    1.12

    the same case after the decimal too.
  • 8 years ago
    Thanks dude, i am really clear about this.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.” - Alan Kay