【问题标题】:Format number in jsp在jsp中格式化数字
【发布时间】:2011-07-27 08:32:09
【问题描述】:

如何将 123456789 的 int 值格式化为 123,456,789

【问题讨论】:

    标签: jsp number-formatting


    【解决方案1】:

    使用 JSTL fmt:formatNumber

    http://download.oracle.com/docs/cd/E17802_01/products/products/jsp/jstl/1.1/docs/tlddocs/fmt/formatNumber.html

    将你的模式设置为#,##0

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <fmt:formatNumber pattern="#,##0" value="${value}" />
    

    这将要求您在 WEB-INF/lib 文件夹中拥有 JSTL 标准标签库

    http://tomcat.apache.org/taglibs/standard/

    现在我不能 100% 确定,但大多数现代容器都提供“核心”API 库 jstl.jar,而您的 Web 应用程序必须提供实现。在上述链接的情况下,应该是下载中包含的standard.jar。

    【讨论】:

    • 您好,我尝试了您的回答,但出现错误“绝对 uri java.sun.com/jsp/jstl/fmt 无法在 web.xml 或 jar 中解析...”
    • 抱歉,我以为您已将 JSTL 添加到您的 Web 应用程序中。
    • @DaveG:您的模式需要纠正。目前它是 '#,###0',应该是 '#,##0'。
    • 不知道,我收到了javax.servlet.jsp.JspTagException: java.lang.IllegalArgumentException: Unexpected '0' in pattern "#,##0"
    • 您使用的是哪个 JSTL?你能提供细节吗?我刚刚构建了一个示例应用程序,它运行良好。如果您愿意,我可以将其发布到 github。
    【解决方案2】:

    试试这个代码

    public class Main {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        DecimalFormat formatter = new DecimalFormat("###,###,###");
        System.out.print(formatter.format(123456789));
    }
    

    }

    你可以在jsp块中写一个函数 和 main 方法中的代码。

    【讨论】:

    • 尽量不要在现代应用程序中使用 scriptlet 。尽可能使用 JSTL。
    【解决方案3】:
    猜你喜欢
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 2012-02-25
    • 2011-09-03
    • 2014-09-10
    相关资源
    最近更新 更多