Search code examples
javaservletscurrencynumber-formattingglobalization

Servlet Globalization NumberFormat.getCurrencyInstance(Locale.JAPAN) doen't work properly


I came across the following issue when I have been practicing on Java Servlet globalization:

NumberFormat.getCurrencyInstance(Locale.UK)

is working perfectly while

NumberFormat.getCurrencyInstance(Locale.JAPAN)

is showing '?' instead of '‎¥'.

Here is my code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Detecting Locale </title>");
        out.println("</head>");
        out.println("<body>");

        long number = 5_000_000L;

        NumberFormat numForUK = NumberFormat.getCurrencyInstance(Locale.UK);
        out.println("<p>Format currency with UK locale: " + numForUK.format(number) + "</p>");

        NumberFormat numForJAPAN = NumberFormat.getCurrencyInstance(Locale.JAPAN);
        out.println("<p>Currency Format using Japan Locale: " + numForJAPAN.format(number));

        out.println("</body>");
        out.println("</html>");
    }

Output on Google Chrome:

Format currency with UK locale: £5,000,000.00

Currency Format using Japan Locale: ?5,000,000

Please help me out to solve the problem. Thank you!


Solution

  • try instead of response.setContentType("text/html"); response.setContentType("text/html; charset=utf-8");