DEV Community

loading...
Cover image for Java - Converting Between Numbers and Strings

Java - Converting Between Numbers and Strings

joshuabrown0010 profile image joshua-brown0010 ・1 min read

In this post, you will learn how to convert from int to String, from float to String, and from String to int or to float. With this you can easily convert between Java data types.

For example, converting a string to an integer, or an integer to a string. For this we are going to use the methods:

  • Integer.parseInt
  • Double.parseInt
  • String.valueof

Lets see how it is done.

From number to string

To convert from double or int to String in Java, we are going to use the String.valueOfalready an overloaded method for various data types.

int integer = 123;
double double = 123,123;
// To convert from number to string:
String integerAsString = String. valueOf (integer);
// Same for the float
Floating StringAsString = String. valueOf (double);
Enter fullscreen mode Exit fullscreen mode

And in this way we will have the number converted to a string.

String to number conversion

For the opposite case, when we want to convert a string to double or int , we invoke Integer.parseIntor Double.parseDoubleto receive the string: Read more

Discussion (0)

pic
Editor guide