【问题标题】:Using Struts2 Tags to Formatting Numbers使用 Struts2 标签格式化数字
【发布时间】:2013-12-28 23:53:37
【问题描述】:

我想在我们的 jsp 页面中格式化一些数字。
首先,我在我的 porperties 中定义了一些资源
format.number.with2Decimal={0,number,#0.00}

......
问题1:
我想知道‘#’和‘0’是什么意思?
0.00,#0.00,##.00,###0.00
谁能告诉我它们之间的区别?谢谢!

问题2:
如果我在操作中定义 BigDecimal 类型 BigDecimal number1;

那么我的页面应该使用一种格式来显示这个值,
1.if number1=null then show -NIL-
2.if number1=0 then show -NIL-
3.if number1>0 then show 1.00,3434.98 .....
请忽略号码

问题3:
将 number1 更改为字符串,
1.if number1=null or empty or blank then show -NIL-
2.if number1=Hello then show Hello ....

你能帮我吗?

【问题讨论】:

    标签: java struts2 format bigdecimal number-formatting


    【解决方案1】:

    给你:

    <s:property value="getText('{0,number,#,##0.00}',{profit})"/>
    

    这就是我在项目中格式化数字的方式。您可以将它与&lt;s:if&gt; 一起使用,以达到您的要求。

    【讨论】:

    • 不工作` `
    • 不工作&lt;s:property value="{0,${rv.fconsume },#,###0.00}"/&gt;
    • @Gank 变量名应该包含在 {} 中而不是 ${}
    【解决方案2】:

    问题1:我想知道'#'和'0'是什么意思? 0.00,#0.00,##.00,###0.00谁能告诉我它们之间的区别?谢谢!

    • 0 表示必须打印一个数字,不管它是否存在
    • # 表示如果存在数字则必须打印,否则省略。

    例子:

        System.out.println("Assuming US Locale: " + 
                                 "',' as thousand separator, " + 
                                 "'.' as decimal separator   ");
    
        NumberFormat nf = new DecimalFormat("#,##0.0##");
        System.out.println("\n==============================");
        System.out.println("With Format (#,##0.0##) ");
        System.out.println("------------------------------");
        System.out.println("1234.0 = " + nf.format(1234.0));
        System.out.println("123.4  = " + nf.format(123.4));
        System.out.println("12.34  = " + nf.format(12.34));
        System.out.println("1.234  = " + nf.format(1.234));
        System.out.println("==============================");
    
        nf = new DecimalFormat("#,000.000");
        System.out.println("\n==============================");
        System.out.println("With Format (#,000.000) ");
        System.out.println("------------------------------");
        System.out.println("1234.0 = " + nf.format(1234.0));
        System.out.println("123.4  = " + nf.format(123.4));
        System.out.println("12.34  = " + nf.format(12.34));
        System.out.println("1.234  = " + nf.format(1.234));
        System.out.println("==============================");
    

    Running Example

    输出:

    Assuming US Locale: ',' as thousand separator, '.' as decimal separator)
    
    ==============================
    With Format (#,##0.0##) 
    ------------------------------
    1234.0 = 1,234.0
    123.4  = 123.4
    12.34  = 12.34
    1.234  = 1.234
    ==============================
    
    ==============================
    With Format (#,000.000) 
    ------------------------------
    1234.0 = 1,234.000
    123.4  = 123.400
    12.34  = 012.340
    1.234  = 001.234
    ==============================
    

    在 Struts2 中,您可以使用来自 ActionSupportgetText() 函数应用这种格式。

    P.S:问题 2 和 3 很琐碎(而且很混乱)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      相关资源
      最近更新 更多