velocity的string转数字,非常有意思:

1、通过java的包装类进行转换

#set($intString = "20")
#set($Integer = 0)
$Integer.parseInt($intString)
#set($tring = "20")
#set($Double = 0.0)
$Double.parseDouble($string)

以上输出:

20

20.0

注意:velocity无法直接使用Integer(或Double),但是可以先定义一个整型变量(Double),使用这个整型变量的parseInt(“string”)转换。

先定义,再使用!

2、可以使用spring中的velocity的numberTool(numberToolAttribute属性):

    <bean id="velocityViewResolver"
class="com.banksteel.erp.web.common.utils.VelocityLayoutViewResolver">
<property name="layoutUrl" value="inc/layout.vm"/>
<property name="cache" value="false"/>
<property name="suffix" value=".vm"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="dateToolAttribute" value="dateTool"/>
<property name="numberToolAttribute" value="numberTool"/>
<property name="requestContextAttribute" value="req"/>
<property name="exposeRequestAttributes" value="true"/>
<property name="exposePathVariables" value="true"/>
<property name="toolboxConfigLocation" value="WEB-INF/tool-box.xml"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
</bean>

$numberTool.format("0.00", "500");

3、NumberTool

org.apache.velocity.tools.generic.NumberTool

在toolbox.xml中配置如下所示:

<tool>
<key>number</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.NumberTool</class>
<parameter name="format" value="#0.0"/>
</tool>

关于number,如下所示:

函数举例显示结果:

$number            org.apache.velocity.tools.generic.NumberTool@32c41c                            $number.currency("35")            ¥35.00                            $number.format            #0.0                            $number.locale            zh_CN                            $number.format("35")            35.0                            $number.integer("35.47")            35                            $number.number("35.47")            35.47                            $number.percent("0.3547")            35%                            $number.toNumber("35.47")            35.47         

以上!

注意:velocity的减法:“-”前后都要有空格!!!

velocity的string转数字,numberTool的更多相关文章

  1. String.Format数字格式化参考

    String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转) 数字 {0:N2} 12.36 数字 {0:N0} 13 货币 {0:c2} $12.36 货币 {0:c4 ...

  2. String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转)

    String.Format数字格式化输出 {:N2} {:D2} {:C2} (转) //格式为sring输出 // Label1.Text = string.Format("asdfads ...

  3. C#:String.Format数字格式化输出 {0:N2} {0:D2} {0:C2}等等

    int a = 12345678; //格式为sring输出//   Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); / ...

  4. C#:String.Format数字格式化输出

    int a = 12345678; //格式为sring输出//   Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);// ...

  5. .net String.Format数字格式化输出

    内容转载自:http://www.cnblogs.com/lqb/archive/2008/08/04/1259498.html 前面内容这个做的总结的很全,今后有新增的我继续往后补充.请留意我增加的 ...

  6. String.Format数字格式化输出 {0:N2} {0:D2} {0:C2

    //格式为sring输出 //   Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); //   Label2.Text = ...

  7. 【转】C# String.Format数字格式化输出各种转换{0:N2} {0:D2} {0:C2}...

    ; //格式为sring输出 // Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); // Label2.Text = & ...

  8. C# string 保留数字英文字母

    using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressi ...

  9. EditText 数字范围 检查string 是不是数字

    public static boolean isNumeric00(String str){ try{ Integer.parseInt(str); return true; }catch(Numbe ...

随机推荐

  1. Active-MQ的安装

    (1)首先就是下载软件 wget http://archive.apache.org/dist/activemq/apache-activemq/5.9.0/apache-activemq-5.9.0 ...

  2. 知道了grunt怎么用了

      第一步: //如何安装指定grunt的插件,安装后自动在当前cmd目录下新建node_modules文件夹,文件夹里面包含了下载完成的插件 npm install <module> - ...

  3. PHP 数据库驱动、连接数据不同方式学习笔记

    相关学习资料 http://www.php.net/manual/zh/refs.database.php http://www.php.net/manual/zh/internals2.pdo.ph ...

  4. android 开发问题:java.lang.ClassCastException

    java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl ...

  5. tomcat8编码

    web工程,本机能跑的代码放到生产环境中后能跑但是得不到预期的结果,十有八九的原因是 编码问题

  6. 锋利的jQuery-4--给事件添加命名空间

    可以把为元素绑定的多个事件用命名空间规范起来. $(function(){ $("p").bind("mouseover.plugin", function() ...

  7. mybatis 使用resultMap实现关联数据的查询(association 和collection )

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  8. jetty使用教程(嵌入eclipse开发)

    在eclipse下面建一个java project 建立目录结构如下: 二级目录: (备注jetty_test是工程的根目录,etc.lib.webRoot为其二级目录) 到jetty的官方网站(ht ...

  9. asp.net缓存(转)

    转自:http://www.cnblogs.com/knowledgesea/archive/2012/06/20/2536603.html 一.缓存概念,缓存的好处.类型.            缓 ...

  10. HDU4891_The Great Pan_字符串水题

    2014多校第五题,当时题面上的10^5写成105,我们大家都wa了几发,改正后我和一血就差几秒…不能忍 题目:http://acm.hdu.edu.cn/showproblem.php?pid=48 ...