Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

velocity

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?

like image 526
Tarun Kumar Avatar asked Oct 30 '25 19:10

Tarun Kumar


1 Answers

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>
like image 151
Nathan Bubna Avatar answered Nov 02 '25 22:11

Nathan Bubna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!