1. <i id='4k6hL'><tr id='4k6hL'><dt id='4k6hL'><q id='4k6hL'><span id='4k6hL'><b id='4k6hL'><form id='4k6hL'><ins id='4k6hL'></ins><ul id='4k6hL'></ul><sub id='4k6hL'></sub></form><legend id='4k6hL'></legend><bdo id='4k6hL'><pre id='4k6hL'><center id='4k6hL'></center></pre></bdo></b><th id='4k6hL'></th></span></q></dt></tr></i><div id='4k6hL'><tfoot id='4k6hL'></tfoot><dl id='4k6hL'><fieldset id='4k6hL'></fieldset></dl></div>
    2. <small id='4k6hL'></small><noframes id='4k6hL'>

      <tfoot id='4k6hL'></tfoot>

          <bdo id='4k6hL'></bdo><ul id='4k6hL'></ul>
      1. <legend id='4k6hL'><style id='4k6hL'><dir id='4k6hL'><q id='4k6hL'></q></dir></style></legend>

        使用 Struts2 标签格式化数字

        时间:2023-05-28

            <bdo id='m63Rl'></bdo><ul id='m63Rl'></ul>
              <i id='m63Rl'><tr id='m63Rl'><dt id='m63Rl'><q id='m63Rl'><span id='m63Rl'><b id='m63Rl'><form id='m63Rl'><ins id='m63Rl'></ins><ul id='m63Rl'></ul><sub id='m63Rl'></sub></form><legend id='m63Rl'></legend><bdo id='m63Rl'><pre id='m63Rl'><center id='m63Rl'></center></pre></bdo></b><th id='m63Rl'></th></span></q></dt></tr></i><div id='m63Rl'><tfoot id='m63Rl'></tfoot><dl id='m63Rl'><fieldset id='m63Rl'></fieldset></dl></div>
            • <small id='m63Rl'></small><noframes id='m63Rl'>

              1. <legend id='m63Rl'><style id='m63Rl'><dir id='m63Rl'><q id='m63Rl'></q></dir></style></legend>

                <tfoot id='m63Rl'></tfoot>
                  <tbody id='m63Rl'></tbody>
                  本文介绍了使用 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 标签格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:语言环境的 Java 日期格式 下一篇:Java 使用特定格式的级别顺序打印二叉树

                  相关文章

                    <i id='CgCJ7'><tr id='CgCJ7'><dt id='CgCJ7'><q id='CgCJ7'><span id='CgCJ7'><b id='CgCJ7'><form id='CgCJ7'><ins id='CgCJ7'></ins><ul id='CgCJ7'></ul><sub id='CgCJ7'></sub></form><legend id='CgCJ7'></legend><bdo id='CgCJ7'><pre id='CgCJ7'><center id='CgCJ7'></center></pre></bdo></b><th id='CgCJ7'></th></span></q></dt></tr></i><div id='CgCJ7'><tfoot id='CgCJ7'></tfoot><dl id='CgCJ7'><fieldset id='CgCJ7'></fieldset></dl></div>

                    <small id='CgCJ7'></small><noframes id='CgCJ7'>

                  1. <tfoot id='CgCJ7'></tfoot>

                        <bdo id='CgCJ7'></bdo><ul id='CgCJ7'></ul>
                      <legend id='CgCJ7'><style id='CgCJ7'><dir id='CgCJ7'><q id='CgCJ7'></q></dir></style></legend>