簡體   English   中英

使用Struts2標簽格式化數字

[英]Using Struts2 Tags to Formatting Numbers

我想在jsp頁面中格式化一些數字。
首先,我在我的porperties中定義了一些資源
format.number.with2Decimal={0,number,#0.00}

......
問題1:
我想知道'#'和'0'是什么意思?
0.00,#0.00,##。00,### 0.00
誰能告訴我他們之間的區別? 謝謝!

問題2:
如果我在我的動作BigDecimal number1中定義一個BigDecimal類型;

然后我的頁面應該使用一種格式來顯示這個值,
1.if number1=null then show -NIL-
2.if number1=0 then show -NIL-
3.if number1>0 then show 1.00,3434.98 .....
請忽略數字<0

問題3:
將number1更改為String,
1.if number1=null or empty or blank then show -NIL-
2.if number1=Hello then show Hello ....

你能幫我個忙嗎?

干得好 :

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

這就是我在項目中格式化數字的方式。 您可以將它與<s:if>一起使用,以達到您的要求。

問題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("==============================");

運行示例

輸出:

 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()函數應用此類格式。

PS:問題2和3是微不足道的(而且很混亂)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM