Skip to content

Instantly share code, notes, and snippets.

@adojos
Last active March 9, 2023 11:09
Show Gist options
  • Save adojos/0fa83bd59d1e1cea2f69be1c557010fc to your computer and use it in GitHub Desktop.
Save adojos/0fa83bd59d1e1cea2f69be1c557010fc to your computer and use it in GitHub Desktop.
Java: Format Decimal Number with Locale #java
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class DecimalFormatWithLocale {
public static void main(String[] args) {
double dbNum = 170180.24523D;
/* Formatting Numbers Using Locale only (No Pattern Specified) */
NumberFormat nFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);
String strOptionalInfo = Locale.ENGLISH.getDisplayName() + " ("+Locale.ENGLISH.toLanguageTag()+")";
DecimalFormat dFormat = (DecimalFormat) nFormat; // cast NumberFormat into DecimalFormat
String strFormattedNum = dFormat.format(dbNum);
System.out.println(strFormattedNum + " - " + strOptionalInfo);
nFormat = NumberFormat.getNumberInstance(Locale.FRANCE);
strOptionalInfo = Locale.FRANCE.getDisplayName() + " ("+Locale.FRANCE.toLanguageTag()+")";
dFormat = (DecimalFormat) nFormat; // cast NumberFormat into DecimalFormat
strFormattedNum = dFormat.format(dbNum);
System.out.println(strFormattedNum + " - " + strOptionalInfo);
nFormat = NumberFormat.getNumberInstance(Locale.GERMAN);
strOptionalInfo = Locale.GERMAN.getDisplayName() + " ("+Locale.GERMANY.toLanguageTag()+")";
dFormat = (DecimalFormat) nFormat; // cast NumberFormat into DecimalFormat
strFormattedNum = dFormat.format(dbNum);
System.out.println(strFormattedNum + " - " + strOptionalInfo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment