How to set thousands separator in Java?
This should work (untested, based on JavaDoc):
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(bd.longValue()));
According to the JavaDoc, the cast in the first line should be save for most locales.
How to change the grouping separator (thousands) of DecimalFormat from comma/point to quote using patterns?
Can you use a subclass of DecimalFormat instead?
You could analyze the pattern in the subclass and extract the unusual grouping separator from the pattern. On a call of the subclass's format method, you can use this grouping separator with the original DecimalFormat as shown in your own example code. This is the result that is returned by the format method of the subclass.
Display number in Textview with thousand separator and decimal format
This did the trick:
DecimalFormat decim = new DecimalFormat("#,###.##");
tv.setText(decim.format(someFloat));
How to format a double into thousand separated and 2 decimals using java.util.Formatter
If you need to use the java.util.Formatter
, this will work:
double amount = 5896324555.59235328;
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);
formatter.format("$ %(,.2f", amount);
System.out.println("Amount is - " + sb);
Expanded on the sample code from the Formatter
page.
Showing real numbers with thousands separator while typing
Did you read the docs for DecimalFormat?
....
0 Number Yes Digit
# Number Yes Digit, zero shows as absent
....
Seems like you want to use the format:
new DecimalFormat("#,###.00");
Related Topics
Creating Hashmap from a JSON String
Android: Textview Automatically Truncate and Replace Last 3 Char of String
What Is the Easiest Way to Get the Current Day of the Week in Android
How to Hash a String in Android
Android: Clickablespan in Clickable Textview
Android HTML.Fromhtml() with Images
Change Icons of Checked and Unchecked for Checkbox for Android
How to Prevent Gson from Expressing Integers as Floats
How to Start Activity in Adapter
Java Lang Unsupportedclassversion Error in Xamarin Studio
Android: How to Parse Url String with Spaces to Uri Object
How to Execute JavaScript on Android
Convert Utc to Current Locale Time
How to Draw a Filled Triangle in Android Canvas
Android:How to Read File in Bytes
Which Jdk Version (Language Level) Is Required for Android Studio