开发者

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

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?

wil开发者_Go百科l 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?


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜