Tarun Kumar
Tarun Kumar

Reputation: 41

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?

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?

Upvotes: 4

Views: 15403

Answers (1)

Nathan Bubna
Nathan Bubna

Reputation: 6933

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>

Upvotes: 14

Related Questions