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!

Dani AI

Generated

Two simple, robust options without hand-rolling string recursion (which breaks on negatives, decimals, and very large values):

// 1) One-liner, locale-aware
String us = String.format(Locale.US, "%,d", number);        // 12,345
String de = String.format(Locale.GERMANY, "%,d", number);   // 12.345

The "," flag inserts the locale-specific grouping separator for you, so you get commas, dots, or spaces as appropriate. It also works with long and BigInteger, and handles negatives. See the official flags table for Formatter. Oracle JDK 21 Formatter docs.

If you are formatting lots of numbers in performance-sensitive code, prefer reusing a formatter but do not share one instance across threads. Both NumberFormat and DecimalFormat are not thread-safe. A lightweight pattern is:

// 2) Reusable, per-thread formatter
private static final ThreadLocal<NumberFormat> US_INT =
    ThreadLocal.withInitial(() -> NumberFormat.getIntegerInstance(Locale.US));

// usage
String s = US_INT.get().format(number);

Oracle documents that number formats are generally not synchronized; create separate instances per thread or guard with external synchronization. Oracle JDK 11 NumberFormat docs (Synchronization).

Notes that complement and :

  • Prefer passing an explicit Locale if you want a specific separator; relying on the default locale can surprise you when code moves machines.
  • Avoid patterns that try to mix grouping sizes (for example, Indian-style "#,##,##0"). In java.text.DecimalFormat the grouping size is constant and, if you supply multiple grouping characters in the pattern, only the interval nearest the end is used. Oracle JDK 8 DecimalFormat docs. If you truly need mixed grouping, use a custom routine or a library designed for it.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.