使用Struts2标签格式化数字

我想在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 ....

你能帮我个忙吗?

问题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是微不足道的(而且很混乱)。

干得好 :

  

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