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

Similar Programs Chapter Last Updated
Java Parse JSON String To Object Miscellaneous 24-03-2023
Java Program For Date And Time Miscellaneous 24-03-2023
Java Binary Search Tree Implementation Miscellaneous 21-03-2023
Java Program To Merge Two PDF Files Miscellaneous 18-03-2023
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Factory Design Pattern In Java Miscellaneous 11-05-2021
Data Types In Java Miscellaneous 09-06-2018

1