Last active
March 9, 2023 11:09
-
-
Save adojos/0fa83bd59d1e1cea2f69be1c557010fc to your computer and use it in GitHub Desktop.
Java: Format Decimal Number with Locale #java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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