Converting between Strings and Numbers(int & float)

  1. Converting numbers --> strings

    The method toString in the wrapper classes can be used to convert value of any fundamental type to string

    If you need information on wrapper classes, then click here

  2. Examples of number --> string conversions:

    int i = 4; double d = 3.14; String s1, s2, s3, s4; s1 = Integer.toString(47); // Converts integer to String s2 = Integer.toString(i); // Converts integer to String s4 = Double.toString(2.87); // Converts double to String s4 = Double.toString(d); // Converts double to String

    Example program: click here

  3. Converting strings --> numbers

    The various "parse..." methods in the wrapper classes can be used to convert a string into value of any fundamental type

  4. Examples of string --> number conversions:

    int i1, i2; double d1, d2; String s1 = "1234", s2 = "3.14"; i1 = Integer.parseInt("49"); // Converts String to integer i2 = Integer.parseInt(s1); // Converts String to integer d1 = Double.parseDouble("2.87"); // Converts String to double d2 = Double.parseDouble(s2); // Converts String to double

    Example program: click here