NumberFormat

The NumberFormat class (from the java.text package) provides methods to format and parse numbers in a locale-sensitive manner. It is commonly used for formatting:

  • General numbers
  • Currency values
  • Percentages

Unlike DecimalFormat, NumberFormat provides default formats based on locale and is ideal for user-facing number displays.

Key Features

  • Locale-aware formatting
  • Formats currency and percentages
  • Parses strings back into numbers
  • Rounds numbers according to locale rules

Commonly Used Methods

Simple Program

import java.text.NumberFormat;
import java.util.Locale;

public class SimpleNumberFormatExample {
    public static void main(String[] args) {
        double amount = 1234567.89;

        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
        String formattedAmount = currencyFormat.format(amount);

        System.out.println("Formatted Amount: " + formattedAmount);
    }
}
/*
Formatted Amount: $1,234,567.89
*/

Problem Statement

Paani and Mahesh are designing a Loan EMI Summary System for a bank. For each customer, the system should display:

  • Customer Name
  • Loan Amount (Formatted as Currency)
  • Interest Rate (Formatted as Percentage)
  • Monthly EMI (Rounded and Formatted as Currency)

Use NumberFormat to make the financial figures user-friendly and localized.

import java.text.NumberFormat;
import java.util.Locale;

class LoanDetails {
    String customerName;
    double loanAmount;
    double interestRate; // in decimal, e.g., 0.075 for 7.5%
    double emi;

    public LoanDetails(String customerName, double loanAmount, double interestRate, double emi) {
        this.customerName = customerName;
        this.loanAmount = loanAmount;
        this.interestRate = interestRate;
        this.emi = emi;
    }
}

public class LoanEMISummary {
    public static void main(String[] args) {
        LoanDetails[] customers = {
            new LoanDetails("Mahesh", 500000.00, 0.075, 10452.34),
            new LoanDetails("LotusJavaPrince", 800000.00, 0.0685, 15600.75),
            new LoanDetails("Priya", 300000.00, 0.0825, 9500.25)
        };

        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
        NumberFormat percentFormat = NumberFormat.getPercentInstance();
        percentFormat.setMinimumFractionDigits(1);

        System.out.println("----- Loan EMI Summary -----");
        System.out.printf("%-20s %-20s %-15s %-15s\n", "Customer", "Loan Amount", "Interest Rate", "Monthly EMI");

        for (LoanDetails ld : customers) {
            String loanAmt = currencyFormat.format(ld.loanAmount);
            String interest = percentFormat.format(ld.interestRate);
            String emi = currencyFormat.format(ld.emi);

            System.out.printf("%-20s %-20s %-15s %-15s\n", ld.customerName, loanAmt, interest, emi);
        }
    }
}
/*
----- Loan EMI Summary -----
Customer             Loan Amount          Interest Rate   Monthly EMI    
Mahesh               ₹5,00,000.00         7.5%            ₹10,452.34     
LotusJavaPrince      ₹8,00,000.00         6.9%            ₹15,600.75     
Priya                ₹3,00,000.00         8.3%            ₹9,500.25
*/

The NumberFormat class makes numerical output more professional and user-friendly, especially for applications involving money, percentages, or general numbers.

This class is vital in:

  • Banking software
  • E-commerce apps
  • Invoice generation
  • Reports and dashboards
Scroll to Top