Results 1 to 3 of 3

Thread: Converting a string to int and back

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2001
    Location
    USA
    Posts
    25

    Converting a string to int and back

    Hey,

    I am having some difficulties converting a string which contains numbers into an integer and then dividing the number by 15 then converting the result back to a string.

    I'm pretty sure I am way off and stuff but this is what I got:
    Code:
    Nums[intX]=(string)Convert.ChangeType((int)Convert.ChangeType(Nums[intX], typeof(int))/15, typeof(string));
    The error that I am getting is: System.FormatException: Input string was not in a correct format.

    I'm probably not using the ChangeType method properly so any help is greatly appriciated.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Try that:
    Nums[intX] = (int)(int.Parse(Nums[intX])/15).toString()

    I haven't verified it works, so if it doesn't, look into the parse() method of the integer object. It will parse a string into a integer.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2001
    Location
    USA
    Posts
    25
    Same error, I did change it so it dumps the result into an integer array so it doesn't need to convert it back to a string.

    This is my new code:
    ActualNums[intX] = (int)(int.Parse(Nums[intX])/15);

    and I still get: System.FormatException: Input string was not in a correct format.

    And I checked and the Nums[] array is a string; however when I try this:
    ActualNums[intX] = (int)(int.Parse("555")/15);

    It works...

    TIA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width