有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

独立于语言的JSP自定义模式的java JSTL formatNumber

我正在用JSP开发一个小应用程序,我需要将欧洲应用程序转换为国际应用程序(与美国格式兼容等等)。我为标记formatNumber{a1}创建了模式选项,但它始终取决于应用程序的语言环境

示例1:

我有一个地区en_US,格式号是:

 <fmt:formatNumber pattern="#,##0.00" value="${number}"/>

结果:15463536640.00

示例2:

我有一个地区es_es,格式号是:

 <fmt:formatNumber pattern="#,##0.00" value="${number}"/>

结果:15.463.536.640,00

这是一种模式,它与语言环境有关!我需要独立于应用程序区域设置使用逗号和点,因为并不总是希望使用区域设置格式来显示数字

有什么帮助吗


共 (3) 个答案

  1. # 1 楼答案

    DecimalFormat

    DecimalFormat formatter = new DecimalFormat("###,###,###");
    
  2. # 2 楼答案

    在您自己的标记文件中:

    • 将locale设置为en_US
    • 用你的图案打印所需的数字
    • 回到老地方

    标记文件如下所示:

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
    <%@attribute name="number" description="The number to format and print." %>
    
    <fmt:setLocale value="en_US"/>
    <fmt:formatNumber pattern="#,##0.00" value="${number}"/>
    <fmt:setLocale value="${user.locale}"/>
    

    或者,如果不想硬编码,可以将模式添加为输入参数

  3. # 3 楼答案

    只需显式设置区域设置

    <!  Page's own locale (you should already have that part).  >
    <fmt:setLocale value="${user.locale}" />
    <fmt:setBundle ... />
    
    ... text ...
    
    <!  Temporarily set to English, format number and then set back to page locale.  >
    <fmt:setLocale value="en_US" />
    <fmt:formatNumber ... />
    <fmt:setLocale value="${user.locale}" />
    

    另请参见: