使用 Struts2 标签格式化数字

Using Struts2 Tags to Formatting Numbers(使用 Struts2 标签格式化数字)

本文介绍了使用 Struts2 标签格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我们的 jsp 页面中格式化一些数字.
首先,我在我的属性中定义了一些资源
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 .....
请忽略数字<0

问题3:
将 number1 更改为字符串,
1.如果 number1=null 或为空或空白则显示 -NIL-
2.if number1=Hello then show Hello ....

你能帮我吗?

解决方案

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

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

例子:

 System.out.println("假设美国语言环境:" +"',' 作为千位分隔符," +'.'作为小数分隔符");NumberFormat nf = new DecimalFormat("#,##0.0##");System.out.println("
===============================");System.out.println("带格式(#,##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("
===============================");System.out.println("带格式(#,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("==============================");

运行示例

输出:

<块引用>

假设美国语言环境:',' 作为千位分隔符,'.'作为小数分隔符)===============================带格式 (#,##0.0##)------------------------------1234.0 = 1,234.0123.4 = 123.412.34 = 12.341.234 = 1.234==============================================================带格式 (#,000.000)------------------------------1234.0 = 1,234.000123.4 = 123.40012.34 = 012.3401.234 = 001.234===============================

在 Struts2 中,您可以使用 ActionSupport 中的 getText() 函数应用这种格式.

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

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}

......
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!

Question2:
if i define a BigDecimal type in my action BigDecimal number1;

Then my page should using a format to show this value,
1.if number1=null then show -NIL-
2.if number1=0 then show -NIL-
3.if number1>0 then show 1.00,3434.98 .....
please ignore number<0

Question3:
change number1 to a String,
1.if number1=null or empty or blank then show -NIL-
2.if number1=Hello then show Hello ....

could you give me help?

解决方案

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

这篇关于使用 Struts2 标签格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 Struts2 标签格式化数字

基础教程推荐