TYPE CONVERSIONS

INT to STRING

It is common to have to convert an integer variable to String.  This is done very easily using String concatenation.

       int i = 42;

       String s = i + "";


In the code above, the String s gets the value “42”.

DOUBLE to STRING

This is very easy and is the exact same process as working with int.
 

       double d = 15.4;

       String s = d + "";


STRING to INT

We can convert a String value to an integer as long as every character in the String is a number.

       String s = "12345";

       int i = Integer.parseInt(s);


Note that even having a space character in the String would cause an error in the conversion above.


STRING to DOUBLE

We can convert a String value to a double value as long as every character is a number and there is no more than one point character.

       String s = "3.1415";

       double d = Double.parseDouble(s);


Note that even having a space character in the String would cause an error in the conversion above.  Similarly, having two points would cause an error.

INT to DOUBLE

 

Java automatically supports the conversion from a double variable to an integer.  So no special work is needed.

 

int i = 4;

double d = i;

 

In the above code, d will get the value of 4.0.

 

DOUBLE to INT (via rounding)

 

One approach to converting a double variable to an integer is to use rounding.  There is a round function in the Math class but note that it returns a long variable.  So we do need to typecast to an integer.

 

double d = 4.25;

int i = (int)Math.round(d);

 

In the above code, the variable i would get the value of 4.

 

DOUBLE to INT (via truncation)

 

We can convert a double to an int by omitting any decimal values all together.  This is called truncation and can be done using a simple typecast.

 

double d = 7.82;

int i = (int)d;

 

In the above code, the variable i would get the value of 7.

 

DOUBLE to DOUBLE with specified precision

 

Ok, so this isn’t a type conversion.  We start with a double and we want another double that has a set number of decimal places.  But it is a form of conversion that could be time consuming to figure out without a hint so we will cover it here.

 

Let’s say we have the number 3.14159265 and we want it with two decimal points only.

 

double d = 3.14159265;

double d2 = (int)Math.round(d*100)/100.0;

 

The above code will store the value 3.14 in d2.

 

And, to get the same number to four decimal places, we use 10000 instead of 100.  See below:

 

double d = 3.14159265;

double d2 = (int)Math.round(d*10000)/10000.0;

 

The above code will store the value 3.1416 in d2.


AN ARRAY to STRING

This is likely rarely needed.  However, we can put the content of an int or double array into a String by using the following:

       int[] arr = {1, 4, 2, 9, 5};

       String s = Arrays.toString(arr);

 

Here is an example:

 

import java.util.Arrays;

 

public class Testing

{

    public static void main(String[] args)

    {

       int[] arr = {2, 5, 8, 3};

       String s = Arrays.toString(arr);

       System.out.println(s);  

    }

}

 

 

The above code will output the following to screen:

 

[2, 5, 8, 3]

 

If you wish to get rid the of the brackets, the following line would create a second String s2 without them:

 

 

String s2 = s.substring(1, s.length()-1);

 

 

Furthermore, if we wanted to remove the commas and the spaces, we could use the replaceAll method for Strings.  The following line creates a String named s3 that has no spaces or commas:

 

 

String s3 = s2.replaceAll(" ", "").replaceAll(",","");