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?
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With