如何格式化 Velocity 模板中的数字?
我在速度模板中得到一个 java 对象。该对象有一个双精度值,我想将其格式化为小数点后两位并将其显示在我的模板中。
我获取对象的类是这样的
Class Price
{
double value;
String currency;
}
在我的速度模板中,我获取这样的值
$price.value
,但我需要在显示它之前将其格式化为小数点后两位。
我想将
23.59004 转换为 23.59
35.7 转换为 35.70
3.0 转换为 3.00
9 转换为 9.00
请告诉我如何在速度模板中做到这一点?我搜索了很多这个,发现我可以使用velocity工具,但没有相关的例子?我可以在模板中使用速度工具吗?
I am getting a java object in my velocity template. The object has a double value which I want to format to 2 decimal places and display it in my template.
The class for which im getting an object is something like this
Class Price
{
double value;
String currency;
}
In my velocity template, im getting the value like this
$price.value
but I need to format it to 2 decimal places before displaying it.
I want to convert
23.59004 to 23.59
35.7 to 35.70
3.0 to 3.00
9 to 9.00
Please tell me how can I do it in velocity template? I searched a lot for this and found that I can use velocity tools, but there are no examples related to it? and can i use velocity tools in templates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Velocity模板中预计会使用Velocity工具;本质上,它们是添加到模板中可用变量的对象,以便您可以使用 $numberTool.format("#0.00", $val) 或类似的。如果没有可用的工具不能满足您的需求,只需创建一个 POJO 并将其添加到模板中即可。
要使其正常工作,您还应该添加以下 Maven 依赖项:
并编写以下代码:
Velocity tools are expected to be used in Velocity templates; essentially they are objects added to the variables available in a template so that you can use
$numberTool.format("#0.00", $val)
or similar. If none of the available tools don't fit your needs, simply create a POJO and add it to the template.To make it working you also should add the following maven dependency:
and write following code:
在这种情况下,
$val
必须是 Double 或 Float...$val
has to be Double or Float in this case...使用 VelocityTools 项目中的 MathTool。
Use the MathTool from the VelocityTools project.
格式货币($值)。这是很好的 Java 速度代码,用于将数字格式化为货币格式。
formatCurrency($value). This is good java velocity code to format a number to currency format.
仅使用 Java 格式化程序的解决方案:(无需额外的库)
以下是如何将时间戳格式化为人类可读的日期。
Solution by just using the Java Formatters: (without additional libraries)
And here is how the timestamp is formatted to human readable date.
$numberTool.format("#0.00", $val)
$numberTool.format("#0.00", $val)
除了使用
$numberTool.format
之外,更好的方法是使用基于MessageFormat
的工具类,它不仅仅可以执行数字操作。例如,我们使用 Struts 特定的MessageTool
,但您可以使用类似ResourceTool
的东西来代替:这样,您就可以在上下文中获取数字,而不仅仅是通过以下方式获取数字 :本身。在非英语语言中,数字可能更适合位于文本的左侧、中间或其他位置。与简单地单独格式化数字相比,这为您提供了更大的灵活性。
A better way to do things besides using
$numberTool.format
is to use one of theMessageFormat
-based tool classes that do more than just numbers. For example, we useMessageTool
which is Struts-specific, but you can use something similar likeResourceTool
instead:This way, you get the number in context and not just all by itself. In a non-English language, the number might be more appropriate to come to the left of the text, or in the middle, or whatever. This gives you much more flexibility than simply formatting the number all by itself.