Using Struts2 Tags to Formatting Numbers

后端 未结 2 1423
轮回少年
轮回少年 2020-12-03 19:48

I want to format some numbers in our jsp pages.
first i define some resources in my porperties
format.number.with2Decimal={0,number,#0.00}

.....

相关标签:
2条回答
  • 2020-12-03 20:00

    Here you go :

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

    This is how I format numbers in my projects. You can use it with <s:if> to attain what you require.

    0 讨论(0)
  • 2020-12-03 20:09

    Question1: i want to know what is the ‘#’ and '0' means? 0.00,#0.00,##.00,###0.00 who can tell me the differences between them? thanks!

    • 0 means that a number must be printed, no matter if it exists
    • # means that a number must be printed if it exists, omitted otherwise.

    Example:

        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

    Output:

    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
    ==============================
    

    In Struts2, you can apply this kind of format with the getText() function from ActionSupport.

    P.S: Question 2 and 3 are trivial (and messy).

    0 讨论(0)
提交回复
热议问题