Locale Specific Currency & Percentage | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Locale Specific Currency

You can use the java.txt.NumberFormat class and its static getCurrencyInstance() method to format a number, such as a long or double type, in a locale specific currency. Following is the example which shows how to format currency specific to a given locale −

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Date;

public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      PrintWriter out = response.getWriter();

      // Get the client's Locale
      Locale locale = request.getLocale( );
      NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
      String formattedCurr = nft.format(1000000);
      System.out.println("formattedCurr : " + formattedCurr);
}

Locale Specific Percentage

You can use the java.txt.NumberFormat class and its static getPercentInstance() method to get locale specific percentage. Following is the example which shows how to format percentage specific to a given locale −

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Date;

public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      PrintWriter out = response.getWriter();
      
      // Get the client's Locale
      Locale locale = request.getLocale( );
      NumberFormat nft = NumberFormat.getPercentInstance(locale);
      String formattedPerc = nft.format(0.51);
      System.out.println("formattedPerc : " + formattedPerc);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s