如何在 Velocity 模板引擎中将十进制数四舍五入到 2 位? [英] How to round off decimal number to 2 places in Velocity template engine?

查看:40
本文介绍了如何在 Velocity 模板引擎中将十进制数四舍五入到 2 位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Velocity 模板引擎中将十进制数四舍五入为 2 位?

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?

Double roundTo(Object decimals, Object num) 可以吗?即

#set ($Percentage = roundTo(2, $Marks*100/$Total)) 会起作用吗?我是否必须在 .vm 文件中包含任何内容才能完成这项工作?

will #set ($Percentage = roundTo(2, $Marks*100/$Total)) work? will I have to include anything in .vm file to make this work?

推荐答案

使用 VelocityTools 项目中的 MathTool.

$math.roundTo(2, $value)

记得把 MathTool 放在你的上下文中:context.put("math", new MathTool()) 或使用 VelocityTools 上下文支持在使用时自动提供工具.

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.

不要忘记为速度添加 maven 依赖 math 工具:

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>

这篇关于如何在 Velocity 模板引擎中将十进制数四舍五入到 2 位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆