How to round off decimal number to 2 places in Velocity template engine?

给你一囗甜甜゛ 提交于 2019-12-10 11:06:22

问题


How can i round off decimal number to 2 places in Velocity Template Engine?

#set ($Percentage = $Marks*100/$Total)

I want to round off Percentage to 2 decimal places. How can i do that?

will Double roundTo(Object decimals, Object num) this work? i.e.

will #set ($Percentage = roundTo(2, $Marks*100/$Total)) work? will I have to include anything in .vm file to make this work?


回答1:


Use the MathTool from the VelocityTools project.

$math.roundTo(2, $value)

remember to put the MathTool in your context: context.put("math", new MathTool()) or use VelocityTools context support to automatically provide tools when you use them.

P.S.

Don't forget adding maven dependency for velocity math tool

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-tools</artifactId>
    <version>2.0</version>
</dependency>


来源:https://stackoverflow.com/questions/7415805/how-to-round-off-decimal-number-to-2-places-in-velocity-template-engine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!