【发布时间】:2017-09-01 06:23:22
【问题描述】:
我在练习 Java Servlet 全球化时遇到了以下问题:
NumberFormat.getCurrencyInstance(Locale.UK)
工作完美,而
NumberFormat.getCurrencyInstance(Locale.JAPAN)
显示“?”而不是 '¥'。
这是我的代码:
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>");
}
Google Chrome 上的输出:
使用英国语言环境格式化货币:5,000,000.00 英镑
使用日本语言环境的货币格式:?5,000,000
请帮我解决问题。 谢谢!
【问题讨论】:
-
查看页面编码。是 UTF-8 吗?
-
'Locale.JAPANESE' 给了我这个输出:使用日本语言环境的货币格式:¤ 5,000,000.00
-
我说的是html页面编码。你在浏览器中看到结果了吗?你能显示你的响应标题吗?
-
来自我的 web.xml
-
尝试代替 response.setContentType("text/html"); response.setContentType("text/html; charset=utf-8");
标签: java servlets currency number-formatting globalization