Converting input strings to integers


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: Converting input strings to integers

  1. #1
    Dave Guest

    Converting input strings to integers


    Is there a standard .NET way to convert input strings to their closest integer
    equivalent? In VB.NET I basically do a "CInt" and it takes care of most of
    the problem (except for null values).

    My first (pathetic non-working) attempt is the following - it currently fails
    on non-numeric input or non-integer numbers. Am I totally out to lunch? Or
    will I need to convert to a char array and look at each character, etc? I'm
    willing to methodically go through that process, so I don't expect anyone
    to write that code for me, but I have a hunch I'm going down the wrong track
    on this. And of course I've named it "CInt" as any VB diehard would have
    done

    public static int CInt(object oValue)
    {
    if (oValue == null || System.Convert.IsDBNull(oValue))
    {
    //default null to 0:
    return 0;
    }

    string sString = oValue.ToString().Trim();
    if (sString == "")
    {
    //default empty string to 0:
    return 0;
    }

    return int.Parse(sString, NumberStyles.Any);
    }


  2. #2
    Russell Jones Guest

    Re: Converting input strings to integers

    Look at the various integer types' Parse method. Or see the Convert.ToInt32
    method (also see Convert to convert to other integer types). Or feel free to
    import the VB namespace and use CInt (and other VB commands) directly.

    "Dave" <dave_doknjas@yahoo.ca> wrote in message
    news:3f319a37$1@tnews.web.devx.com...
    >
    > Is there a standard .NET way to convert input strings to their closest

    integer
    > equivalent? In VB.NET I basically do a "CInt" and it takes care of most of
    > the problem (except for null values).
    >
    > My first (pathetic non-working) attempt is the following - it currently

    fails
    > on non-numeric input or non-integer numbers. Am I totally out to lunch? Or
    > will I need to convert to a char array and look at each character, etc?

    I'm
    > willing to methodically go through that process, so I don't expect anyone
    > to write that code for me, but I have a hunch I'm going down the wrong

    track
    > on this. And of course I've named it "CInt" as any VB diehard would have
    > done
    >
    > public static int CInt(object oValue)
    > {
    > if (oValue == null || System.Convert.IsDBNull(oValue))
    > {
    > //default null to 0:
    > return 0;
    > }
    >
    > string sString = oValue.ToString().Trim();
    > if (sString == "")
    > {
    > //default empty string to 0:
    > return 0;
    > }
    >
    > return int.Parse(sString, NumberStyles.Any);
    > }
    >




  3. #3
    Dave Guest

    Re: Converting input strings to integers


    "Russell Jones" <arj1@northstate.net> wrote:
    >Look at the various integer types' Parse method. Or see the Convert.ToInt32
    >method (also see Convert to convert to other integer types). Or feel free

    to
    >import the VB namespace and use CInt (and other VB commands) directly.
    >
    >"Dave" <dave_doknjas@yahoo.ca> wrote in message
    >news:3f319a37$1@tnews.web.devx.com...
    >>
    >> Is there a standard .NET way to convert input strings to their closest

    >integer
    >> equivalent? In VB.NET I basically do a "CInt" and it takes care of most

    of
    >> the problem (except for null values).
    >>
    >> My first (pathetic non-working) attempt is the following - it currently

    >fails
    >> on non-numeric input or non-integer numbers. Am I totally out to lunch?

    Or
    >> will I need to convert to a char array and look at each character, etc?

    >I'm
    >> willing to methodically go through that process, so I don't expect anyone
    >> to write that code for me, but I have a hunch I'm going down the wrong

    >track
    >> on this. And of course I've named it "CInt" as any VB diehard would have
    >> done
    >>
    >> public static int CInt(object oValue)
    >> {
    >> if (oValue == null || System.Convert.IsDBNull(oValue))
    >> {
    >> //default null to 0:
    >> return 0;
    >> }
    >>
    >> string sString = oValue.ToString().Trim();
    >> if (sString == "")
    >> {
    >> //default empty string to 0:
    >> return 0;
    >> }
    >>
    >> return int.Parse(sString, NumberStyles.Any);
    >> }
    >>

    >

    I'm currently using the int type's Parse method from within my routine, but
    the problem is (and also with the Convert namespace) is that I'll have to
    resort to catching exceptions in this process and I'd rather not do that
    as part of normal logic.

    Import the VB namespace? *** GASP ***
    Ok, maybe, if I have to.



  4. #4
    Len Weaver Guest

    Re: Converting input strings to integers

    Hello Dave,

    > Is there a standard .NET way to convert input strings to their closest

    integer
    > equivalent? In VB.NET I basically do a "CInt" and it takes care of

    most of
    > the problem (except for null values).


    Try the static Double.TryParse method.

    using System.Globalization;

    bool success;
    int intValue;
    double doubleValue;
    string x = "1234";

    success = Double.TryParse( x, NumberStyles.Integer,
    CultureInfo.CurrentCulture,
    out doubleValue )

    if( success ) intValue = (int)doubleValue;

    If it can't perform the conversion it will return false, which is
    better than throwing an exception.

    Hope this helps,
    Len



  5. #5
    Dave Guest

    Re: Converting input strings to integers


    "Len Weaver" <len.weaver@meritsoft.net> wrote:
    >Hello Dave,
    >
    >> Is there a standard .NET way to convert input strings to their closest

    >integer
    >> equivalent? In VB.NET I basically do a "CInt" and it takes care of

    >most of
    >> the problem (except for null values).

    >
    > Try the static Double.TryParse method.
    >
    >using System.Globalization;
    >
    >bool success;
    >int intValue;
    >double doubleValue;
    >string x = "1234";
    >
    >success = Double.TryParse( x, NumberStyles.Integer,
    > CultureInfo.CurrentCulture,
    > out doubleValue )
    >
    >if( success ) intValue = (int)doubleValue;
    >
    > If it can't perform the conversion it will return false, which is
    >better than throwing an exception.
    >
    >Hope this helps,
    >Len
    >
    >

    Perfect!! Thanks Len!
    I couldn't find anything relevant for the int type, so I didn't even bother
    to look at the double type. This is exactly the kind of thing I was looking
    for.



  6. #6
    Join Date
    Feb 2006
    Posts
    1
    thanx Len, i'm kinda n00b in VC# porting myself from VB6. This is exactly what i was looking 4.

  7. #7
    Join Date
    Apr 2004
    Location
    Lafayette, LA
    Posts
    200
    You could also use the IsNumeric method, but that would also require importing VB.

  8. #8
    Join Date
    Dec 2005
    Location
    South of Nowhere, Maine
    Posts
    309
    Unfortunately the TryParse is only available for Double. I wish it was available for other conversions...

  9. #9
    Join Date
    Dec 2007
    Posts
    41

    Smile

    Thanks Len
    I was searching for the same.

    Is the similar way available for converting String array to Double array ?

    Actually I used

    Code:
    Dim values() As String = New String() {"3", "4", "5", "6"}
    Dim returnValue() As Double = New Double() {}
    returnValue = Array.ConvertAll(values, New Converter(Of String, Double)(AddressOf StringToDouble))
    
    Public Shared Function StringToDouble(ByVal str As String) As Double
            Dim success As Boolean
            Dim doubleValue As Double
            success = Double.TryParse(Str, doubleValue)
            If success = True Then
                Return doubleValue
            Else
                Return 0
            End If
    End Function
    Do you know any other effective way ?

  10. #10
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Is this way working for you or are you getting errors?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  11. #11
    Join Date
    Dec 2007
    Posts
    41

    Thumbs up

    Hack, its working properly.

  12. #12
    Join Date
    Feb 2004
    Posts
    27
    Try the following as a c# IsNumeric Function:

    private bool IsNumeric(object oTst)
    {
    double nDbl = new double();
    string strTst = Convert.ToString(oTst);
    bool bNumeric = double.TryParse(strTst,
    System.Globalization.NumberStyles.Any, null, out nDbl);
    return bNumeric;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center