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

12,597

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>
Share:
12,597
Admin
Author by

Admin

Updated on June 22, 2022

Comments

  • Admin
    Admin about 2 years

    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?