1
2long l = 12345L;
3String str = Long.toString(l);
4System.out.println(str); //prints '12345'
5
1
2long l = 12345L;
3String str = new StringBuilder().append(l).toString();
4
1
2long l = 0x11L;
3String str = Long.toString(l);
4System.out.println(str); //prints '17'
5l = 011L;
6str = Long.toString(l);
7System.out.println(str); //prints '9'
8
1
2package com.journaldev.string;
3
4import java.text.DecimalFormat;
5
6public class JavaLongToString {
7
8 @SuppressWarnings("deprecation")
9 public static void main(String[] args) {
10 long l = 12345L;
11 String str = Long.toString(l);
12 System.out.println(str);
13
14 str = String.valueOf(l);
15 System.out.println(str);
16
17 // deprecated from Java 9, use valueOf for better performance
18 str = new Long(l).toString();
19 System.out.println(str);
20
21 str = String.format("%d", l);
22 System.out.println(str);
23
24 str = l + "";
25 System.out.println(str);
26
27 str = DecimalFormat.getNumberInstance().format(l);
28 System.out.println(str);
29
30 str = new DecimalFormat("#").format(l);
31 System.out.println(str);
32
33 str = new StringBuilder().append(l).toString();
34 System.out.println(str);
35 }
36}
37
1
2long l = 12345L;
3//deprecated from Java 9, use valueOf for better performance
4String str = new Long(l).toString(); // str is '12345'
5
1
2long number = 45;
3System.out.println(Long.toBinaryString(number)); //101101
4System.out.println(Long.toOctalString(number)); //55
5System.out.println(Long.toHexString(number)); //2d
6System.out.println(Long.toString(number, 5)); //140
7
1
2long l = 12345L;
3String str = DecimalFormat.getNumberInstance().format(l);
4System.out.println(str); //str is '12,345'
5//if you don't want formatting
6str = new DecimalFormat("#").format(l);
7System.out.println(str); //str is '12345'
8