How to convert string to int

edited November 2016 in How To...

Hi

it's easy, but I don't the proper funtion which is iseful for that. An example, please

Thanks

Tagged:

Answers

  • edited November 2016 Answer ✓

    int(String s) in Processing or else Integer.parseInt(String s) in Java should do the job. Example:

    void setup(){
      int a = int("128");
      a++;
      println(a);
    }
    

    P.S. I know that both the functions can be used with any arguments, not just Strings, but I have not mentioned here as you seem to be a beginner.
    Note: int(String s) is exactly the same as Integer.parseInt(String s) .

  • Hi

    I guess that a++ wasn't necessary

    Thanks.....

  • Hi

    What would it be the opposite, so, int to string? if you don't matter?

    Thanks

  • Hi

    Maybe including float to string

    Thanks

  • edited November 2016 Answer ✓

    Please see the conversion section of the reference. It lists all of the functions for converting from one type to another. If you have a specific question about something you don't understand, then please post some example code that shows where you're stuck.

  • Answer ✓

    str()

  • edited November 2016 Answer ✓

    I used that a++ just to show that it works, otherwise my code would be this println(int("128")) which is kind of stupid.
    Excellent answer from KevinWorkman, I never knew that even existed.

  • Hi

    I asked because I didn't find such an obvious question, but somehow after searching

    float tf1; tf1=hundredths+(seconds*100); tf1=tf1/100; sf1 = Float.toString(tf1); //Total time

    In reference I found this

    int i = 65; float f = float(i); println(i + " : " + f); // Prints "65 : 65.0"

    Both of them works

    Thanks

Sign In or Register to comment.