Java Long type Adding Commas

Hey There Experts

I have two large numbers that I am storing in variables of long type.  I need to insert commas to output xx,xxx,xxx,xxx,xxx,xxx.  However, I cannot seem to find how to accomplish this.  I thought I was on the right track but found I was not allowed to do .length on my long variable holding my number.  

Can anyone point me in the direction I need to go to put commas in a variable of type long?

Thanks,
jimbo99999
Jimbo99999Asked:
Who is Participating?
 
for_yanCommented:





Based on link provided by objects:

     long value = 923456789123L;

DecimalFormat myFormatter = new DecimalFormat("###,###");
String output = myFormatter.format(value);

        System.out.println(output);

Open in new window


Output:
923,456,789,123

Open in new window

0
 
objectsCommented:
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
CEHJCommented:
Easier to do
String s = String.format("%,d", value);

Open in new window

0
 
grouper15Commented:
Also

 long n = 461012;
    System.out.format("%d%n", n);
    System.out.format("%08d%n", n);
    System.out.format("%+8d%n", n);
    System.out.format("%,8d%n", n);
    System.out.format("%+,8d%n%n", n);
0
 
msk_apkCommented:
you could use StringBuilder. for example if you have all longs in an array,

StringBuilder builder = new StringBuilder();
long[] array = new long[];
for(int i=0;i<array.length;i++)
{
     builder.append(array[i]);
    if(i != array.length)
    { 
         builder.append(",");
    }
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.