I'd recommend using the java.text package:
double money = 100.1;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String moneyString = formatter.format(money);
System.out.println(moneyString);
This has the added benefit of being locale specific.
But, if you must, truncate the String you get back if it's a whole dollar:
if (moneyString.endsWith(".00")) {
int centsIndex = moneyString.lastIndexOf(".00");
if (centsIndex != -1) {
moneyString = moneyString.substring(1, centsIndex);
}
}
Answer from duffymo on Stack OverflowI'd recommend using the java.text package:
double money = 100.1;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String moneyString = formatter.format(money);
System.out.println(moneyString);
This has the added benefit of being locale specific.
But, if you must, truncate the String you get back if it's a whole dollar:
if (moneyString.endsWith(".00")) {
int centsIndex = moneyString.lastIndexOf(".00");
if (centsIndex != -1) {
moneyString = moneyString.substring(1, centsIndex);
}
}
double amount =200.0;
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
System.out.println(currencyFormatter.format(amount));
or
double amount =200.0;
System.out.println(NumberFormat.getCurrencyInstance(new Locale("en", "US"))
.format(amount));
The best way to display currency
Output
$200.00
Note: Locale constructors have been deprecated. See Obtaining a Locale for other options.

So, since Locale constructors are deprecated, we can use Locale.Builder() to construct a Locale object.
double amount =200.0;
Locale locale = new Locale.Builder().setLanguage("en").setRegion("US").build();
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
System.out.println(currencyFormatter.format(amount));
or
double amount =200.0;
System.out.println(NumberFormat.getCurrencyInstance(new Locale.Builder().setLanguage("en").setRegion("US").build()).format(amount));
Output
$200.00
If you don't want to use sign use this method
double amount = 200;
DecimalFormat twoPlaces = new DecimalFormat("0.00");
System.out.println(twoPlaces.format(amount));
200.00
This also can be use (With the thousand separator )
double amount = 2000000;
System.out.println(String.format("%,.2f", amount));
2,000,000.00
question: How to format digits into dollar amounts
format - Formatting Currencies in Foreign Locales in Java - Stack Overflow
[JAVA] Trouble with Formatting Currency with 2 Decimal Points - Not a Statement Error
How can I convert currency format to a different currency?
Videos
Im doing some homework thats due in a few weeks and i was just wondering, i have to display some doubles to the hundredth place for dollar amounts, some doubles, which should be $5.00 are only showing up as $5.0, and some doubles, lets just say 5.23423523523 i want to be $2.23, any tips?
Try using setCurrency on the instance returned by getCurrencyInstance(Locale.GERMANY)
Broken:
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.GERMANY);
System.out.println(format.format(23));
Output: 23,00 €
Fixed:
java.util.Currency usd = java.util.Currency.getInstance("USD");
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.GERMANY);
format.setCurrency(usd);
System.out.println(format.format(23));
Output: 23,00 USD
I would add to answer from les2 https://stackoverflow.com/a/7828512/1536382 that I believe the number of fraction digits is not taken from the currency, it must be set manually, otherwise if client (NumberFormat) has JAPAN locale and Currency is EURO or en_US, then the amount is displayed 'a la' Yen', without fraction digits, but this is not as expected since in euro decimals are relevant, also for Japanese ;-).
So les2 example could be improved adding format.setMaximumFractionDigits(usd.getDefaultFractionDigits());, that in that particular case of the example is not relevant but it becomes relevant using a number with decimals and Locale.JAPAN as locale for NumberFormat.
java.util.Currency usd = java.util.Currency.getInstance("USD");
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(
java.util.Locale.JAPAN);
format.setCurrency(usd);
System.out.println(format.format(23.23));
format.setMaximumFractionDigits(usd.getDefaultFractionDigits());
System.out.println(format.format(23.23));
will output:
USD23
USD23.23
In NumberFormat code something similar is done for the initial/default currency of the format, calling method DecimalFormat#adjustForCurrencyDefaultFractionDigits. This operation is not done when the currency is changed afterwards with NumberFormat.setCurrency