Skip to content

Instantly share code, notes, and snippets.

@davidauza-engineer
Created April 5, 2019 21:18
Show Gist options
  • Save davidauza-engineer/1b3a2740090878d29a9b8e6a1e9854c2 to your computer and use it in GitHub Desktop.
Save davidauza-engineer/1b3a2740090878d29a9b8e6a1e9854c2 to your computer and use it in GitHub Desktop.
Console program in java showing the use of NumberFormat, Locale, and DecimalFormat classes.
package engineer.davidauza;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
double doubleValue = 1_234_567.89;
NumberFormat numberFormat = NumberFormat.getNumberInstance();
System.out.println(numberFormat.format(doubleValue));
NumberFormat intFormat = NumberFormat.getIntegerInstance();
System.out.println(intFormat.format(doubleValue));
numberFormat.setGroupingUsed(false);
System.out.println(numberFormat.format(doubleValue));
Locale locale = new Locale("de", "DE");
NumberFormat localFormat = NumberFormat.getNumberInstance(locale);
System.out.println(localFormat.format(doubleValue));
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale);
System.out.println(currencyFormat.format(doubleValue));
DecimalFormat df = new DecimalFormat("$##0.00#");
System.out.println(df.format(1));
System.out.println(df.format(5.891));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment