Currency Formatter In Java | How To Format Currency In Java
Chapter:
Miscellaneous
Last Updated:
19-07-2021 06:57:31 UTC
Program:
/* ............... START ............... */
import java.text.NumberFormat;
import java.util.Locale;
public class CurrencyFormatterJava {
public static void main(String[] args) throws Exception {
double num = 45454.65;
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
System.out.println("US Dollar format: " + defaultFormat.format(num));
// If you want to specifically format currency
Locale s = new Locale("sv", "SE");
NumberFormat format = NumberFormat.getCurrencyInstance(s);
System.out.println("Swedish currency after Format: " + format.format(num));
}
}
/* ............... END ............... */
Output
US Dollar format: $45,454.65
Swedish currency after Format: 45 454,65 kr
Notes:
-
By using NumberFormat you can set the currency format , by default it is US dollar format.
- If you want to specifically set the currency format you can use Locale , in above program it is setting swedish currency format.Then numberformat will change to swedish currency and print the format accordingly.
- Please refer the program for more details regarding the currency format. Like this you can set any other country currency also.
Tags
Currency Formatter In Java, How to Format Number as Currency String in Java , NumberFormat in java