0

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!

4
Contributors
6
Replies
10
Views
7 Years
Discussion Span
Last Post by micahli123
0

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());
    }
0
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));
	}
}
0

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));
	}
}
0

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!!

0

use

NumberFormat nf = NumberFormat.getInstance();

or

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

then

nf.format(number);

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.