Java – Converting Between Numbers and Strings

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

原文链接:Java – Converting Between Numbers and Strings

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容