hello Folks,

I would really appreciate if I could get some help on this.

I have an Integer lets say number = 12345
I want to get a string out which is 12,345
so basically puts a commas after every three digits... the number of digits in the number can vary

Thanks in Advance!

turns out this works as expected. But is there something using the numberformat class

private String insertCommas(String str)
    {
        if(str.length() < 4){
            return str;
        }
        return insertCommas(str.substring(0, str.length() - 3)) + "," + str.substring(str.length() - 3, str.length());
    }
import java.text.*;

public class A {
	public static void main(String args[]) {
		int num=12345;
		DecimalFormat df = new DecimalFormat();
		DecimalFormatSymbols dfs = new DecimalFormatSymbols();
		dfs.setGroupingSeparator('.');
		
		df.setDecimalFormatSymbols(dfs);
		System.out.println(df.format((int)num));
	}
}

Thanks a lot! it worked with comma as a separator

I tried to use the setGroupingSeparator but could not get the java recognize that...turns out I was trying to do that for the decimal format (df) and not on decimalformatsymbols (dfs)

thanks again!

import java.text.*;

public class A {
	public static void main(String args[]) {
		int num=12345;
		DecimalFormat df = new DecimalFormat();
		DecimalFormatSymbols dfs = new DecimalFormatSymbols();
		dfs.setGroupingSeparator('.');
		
		df.setDecimalFormatSymbols(dfs);
		System.out.println(df.format((int)num));
	}
}

I was looking for a way to resolve setting a formatted number in a number exactly the same as Solahere in a way to search through web sites.

Thanks a lot, too.
Really useful info for me as well to go on my task!!

use

NumberFormat nf = NumberFormat.getInstance();

or

NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);

then

nf.format(number);

use

NumberFormat nf = NumberFormat.getInstance();

or

NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);

then

nf.format(number);

This question has already been answered. Start a new discussion instead.