forked from learning-zone/java-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerToStringConversions.java
More file actions
50 lines (37 loc) · 1.45 KB
/
IntegerToStringConversions.java
File metadata and controls
50 lines (37 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package strings;
import java.text.DecimalFormat;
public class IntegerToStringConversions {
public static void main(String[] args) {
int num1 = 12345;
int num2 = -12345;
String str1 = Integer.toString(num1);
String str2 = Integer.toString(num2);
System.out.println("String str1: " + str1);
System.out.println("String str2: " + str2);
// Convert using String.valueOf(int)
String str3 = String.valueOf(num1);
System.out.println("Using valueOf(): " + str3);
// Convert using Integer(int).toString()
String str4 = new Integer(num1).toString();
System.out.println("Integer to String: " + str4);
// Convert using DecimalFormat
DecimalFormat df = new DecimalFormat("#,###");
String str5 = df.format(num1);
System.out.println("Decimal Format: "+str5);
// Convert using StringBuffer to StringBuilder
String str6 = new StringBuffer().append(num1).toString();
System.out.println("StringBuffer to StringBuilder: "+ str6);
// Convert with special radix
String bs = Integer.toBinaryString(5);
System.out.println("Integer to Binary: " + bs);
// Convert Decimal to Octal
String os = Integer.toOctalString(255);
System.out.println("Decimal to Octal: " + os);
// Convert Decimal to Hexadecimal
String hs = Integer.toHexString(255);
System.out.println("Decimal to Hexadecimal: " + hs);
// Custom Base/Radix
String cs = Integer.toString(num1, 7);
System.out.println("Decimal to custom Base: " + cs);
}
}