Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

Java - convert int to String

0 contributions
7 points

In Java, we can convert int to String in couple of different ways.

Short solutions:

// solution 1
String str1 = String.valueOf(123); // 123

// solution 2
String str2 = Integer.toString(123); // 123

// solution 3
String str3 = "" + 123; // 123

1. Using String.valueOf()

public class Example1 {

    public static void main(String[] args) {
        int num = 123;
        String str = String.valueOf(num);
        System.out.println(str); // 123
    }
}

Output:

123

2. Using Integer.toString()

public class Example2 {

    public static void main(String[] args) {
        int num = 123;
        String str = Integer.toString(num);
        System.out.println(str); // 123
    }
}

Output:

123

3. Using "" + 123

public class Example3 {

    public static void main(String[] args) {
        int num = 123;
        String str = "" + num;
        System.out.println(str); // 123
    }
}

Output:

123

4. Using String.format()

public class Example4 {

    public static void main(String[] args) {
        int num = 123;
        String str = String.format("%d", num);
        System.out.println(str); // 123
    }
}

Output:

123

5. Using StringBuilder

public class Example5 {

    public static void main(String[] args) {
        int num = 123;
        StringBuilder sb = new StringBuilder();
        sb.append(num);
        String str = sb.toString();
        System.out.println(str); // 123
    }
}

Output:

123

The same can be applied to StringBuffer

6. Using DecimalFormat

import java.text.DecimalFormat;

public class Example6 {

    public static void main(String[] args) {
        int num = 123;
        DecimalFormat df = new DecimalFormat("#");
        String str = df.format(num);
        System.out.println(str); // 123
    }
}

Output:

123

7. Using DecimalFormat with floating point pattern

import java.text.DecimalFormat;

public class Example7 {

    public static void main(String[] args) {
        int num = 12345;
        DecimalFormat df = new DecimalFormat("#,##0");
        String str = df.format(num);
        System.out.println(str); // 12,345
    }
}

Output:

12,345

References

  1. Integer.toString(int)
  2. String.valueOf(int)
  3. String.format(java.lang.String,java.lang.Object...)
  4. StringBuilder.append(int)
  5. DecimalFormat.format(long)
  6. Type conversion - wik

Post summary image

Java - convert int to String - Post summary image - dirask.com - https://dirask.com/q/X13b9j

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more