使用Struts2标签来格式化数字 [英] Using Struts2 Tags to Formatting Numbers

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

问题描述

我想在我们的jsp页面中格式化一些数字。

首先我定义一些资源在我的porperties中
format.number.with2Decimal = {0 ,数字,#0.00}



......

问题1:

i想知道'#'和'0'是什么意思?

0.00,#0.00,##。00,### 0.00

谁能告诉我之间的区别他们?谢谢!


$ b $问题2:

如果我在我的动作中定义了一个BigDecimal类型
BigDecimal number1;

然后我的页面应该使用一个格式来显示这个值,

1.if number1 = null然后显示-NIL -

2.if number1 = 0然后显示-NIL -

3.if number1> 0然后显示1.00,3434.98 .....

请忽略号码<0



问题3:
将number1更改为String,

1.if number1 = null或为空或空白,然后显示-NIL -

2.if number1 =你好然后显示你好....



你能给我的帮助?

解决方案


Question1:我想知道什么是'和' 0 '是指?
0.00 #0.00 ##。00 ### 0.00 谁能告诉我他们之间的区别?




  • 0 表示一个数字必须打印,不管是否存在

  • li>


示例:

  System.out .println(假设美国语言环境:+ 
','千分隔符,+
'。'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(==============================);



b blockquote>

 假设US Locale:','为千位分隔符,'。'为小数点分隔符)

====== ========================
格式(#,## 0.0 ##)
------- -----------------------
1234.0 = 1,234.0
123.4 = 123.4
12.34 = 12.34
1.234 = 1.234
==============================

======= =======================
格式(#,000.000)
------------ ------------------
1234.0 = 1,234.000
123.4 = 123.400
12.34 = 012.340
1.234 = 001.234
==============================


在Struts2中,您可以将这种格式与 getText()函数一起应用于 ActionSupport



PS:问题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("\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).

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆