info@meritcampus.com    +91-85006-22255
...
`

Siva Nookala - 06 Oct 2016
 In programming we will come across a situation where we need to convert number to string and string to number. Converting Numbers to String: There are many ways of converting numbers into string object, By concatenating number with empty string i.e. `" + "` operator The `valueOf()` method converts the given data type to `String`. Each of the `Number` subclass includes a class method, `toString`, that will convert its primitive type in to a `String`. Converting Numbers to StringCODE Try it Online`class NumberstoStringTest{    public static void main(String arg[])    {        int i = 32;        String s1 = "" + i; // LINE A        System.out.println("s1 = " + s1);                double d = 876.54;        String s2 = String.valueOf(d); // LINE B        System.out.println("s2 = " + s2);                float f = 156.8f;        String s3 = Float.toString(f); // LINE C        System.out.println("s3 = " + s3);                String s4 = "" + i + d; // LINE D                System.out.println("s4 = " + s4);        int n = s4.indexOf('.');        System.out.println(n + " Digits before decimal point of s4 string.");        System.out.println(s4.length() - n - 1                + " Digits after decimal point of s4 string.");        }}`OUTPUTs1 = 32s2 = 876.54s3 = 156.8s4 = 32876.545 Digits before decimal point of s4 string.2 Digits after decimal point of s4 string.DESCRIPTIONIn this program, at `LINE A`, `int` is converted into `String` by concatenating with empty string, At `LINE B` `double` is converted into `String` using `valueOf` method, At `LINE C` `float` is converted into `String` using `toString` method and finally at `LINE D`, we are concatenating empty String, `integer` and `double`. We are counting number of digits before and after the decimal point, in the resultant String.THINGS TO TRYApply `valueOf` method to different data types as `String.valueOf(datatype)`. Apply `toString` method to all the data types as `Integer.toString(integer_Datatype), Double.toString(double_datatype)` etc., Try to perform operations on the resultant strings. Converting Strings to Number: The two ways of converting Strings into specified data type are, The `valueOf()` method converts the `String` to specified data type. The parsing methods are widely used to convert strings into numbers. Converting Strings to NumberCODE Try it Online`class StringstoNumberTest{    public static void main(String arg[])    {        String s1 = "25";        int i1 = Integer.valueOf(s1); // LINE E        int i2 = Integer.parseInt(s1); // LINE F        System.out.println(i1 + " " + i2);                float f1 = Float.valueOf(s1);        float f2 = Float.parseFloat(s1);        System.out.println(f1 + " " + f2);                double d1 = Double.valueOf(s1);        double d2 = Double.parseDouble(s1);        System.out.println(d1 + " " + d2);                System.out.print("The sum of all numbers is : ");        System.out.println(i1 + i2 + f1 + f2 + d1 + d2); // LINE G                String s2 = "ABCD";        char c1[] = new char[s2.length()];        for (int i = 0; i < s2.length(); i++) {            c1[i] = Character.valueOf(s2.charAt(i)); // LINE H        }        for (int j = 0; j < c1.length; j++) {            System.out.print(c1[j] + " ");        }        }}`OUTPUT25 2525.0 25.025.0 25.0The sum of all numbers is : 150.0A B C DDESCRIPTIONIn this program, at `LINE E` and `LINE F`, string `s1` is converted into integer by using `valueOf`, `parseInt` methods respectively. Similarly `float` and `double` are converted from string `s1`. At `LINE G` all 6 numbers sum is printed. At `LINE H` the string `s2` containing `ABCD` is converted into character array and all characters are displayed.THINGS TO TRYTry for the below code. `double d = Double.parseDouble("10");System.out.println(d);` The output should be 10.0, since we are using `parseDouble` and it is assigned to datatype `double`. Try for the below code. `int i = Integer.parseInt("1010", 2);System.out.println(i);` The output will be 10, since here the `paseInt` method returns an integer value of the given string representation of binary radix. Try `parseInt(String s , int radix)` for decimal, Octal and Hexadecimal radices.
0
Wrong
Score more than 2 points