简体   繁体   English

如何格式化 Velocity 模板中的数字?

[英]How to format Numbers in Velocity Templates?

I am getting a java object in my velocity template.我在我的速度模板中得到一个 java 对象。 The object has a double value which I want to format to 2 decimal places and display it in my template.该对象有一个 double 值,我想将其格式化为 2 个小数位并将其显示在我的模板中。

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.但我需要在显示之前将其格式化为 2 个小数位。

I want to convert我想转换

23.59004 to 23.59 23.59004 至 23.59

35.7 to 35.70 35.7 到 35.70

3.0 to 3.00 3.0 到 3.00

9 to 9.00 9 到 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?我可以在模板中使用速度工具吗?

Velocity tools are expected to be used in Velocity templates; Velocity 工具预计将用于 Velocity 模板; essentially they are objects added to the variables available in a template so that you can use $numberTool.format("#0.00", $val) or similar.本质上,它们是添加到模板中可用变量的对象,以便您可以使用$numberTool.format("#0.00", $val)或类似的。 If none of the available tools don't fit your needs, simply create a POJO and add it to the template.如果没有可用的工具不适合您的需要,只需创建一个 POJO 并将其添加到模板中。

To make it working you also should add the following maven dependency:为了使其工作,您还应该添加以下 maven 依赖项:

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-tools</artifactId>
    <version>2.0</version>
</dependency>

and write following code:并编写以下代码:

context.put("numberTool", new NumberTool());
#set($String = "abc")
$String.format("%.2f", $val)

$val has to be Double or Float in this case...在这种情况下, $val必须是 Double 或 Float ......

使用 VelocityTools 项目中的 MathTool。

$math.roundTo(2, $val)

formatCurrency($value).格式货币($value)。 This is good java velocity code to format a number to currency format.这是将数字格式化为货币格式的良好 Java 速度代码。

$numberTool.format("#0.00", $val)

A better way to do things besides using $numberTool.format is to use one of the MessageFormat -based tool classes that do more than just numbers.除了使用$numberTool.format之外,一种更好的处理方式是使用基于MessageFormat的工具类之一,它不仅可以执行数字操作。 For example, we use MessageTool which is Struts-specific, but you can use something similar like ResourceTool instead:例如,我们使用 Struts 特定的MessageTool ,但您可以使用类似ResourceTool类的东西:

resources.properties
some.key=The price is currently {0,number,$#.##}

template.vm
<p>
  $msg.get('some.key', 'resources', [$price])
</p>

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.与简单地单独格式化数字相比,这为您提供了更大的灵活性。

Solution by just using the Java Formatters: (without additional libraries)仅使用 Java Formatters 的解决方案:(无需额外的库)

NumberFormat decimalFormat = new DecimalFormat("#.00");
velocityContext.put("decimalFormat", decimalFormat);

Int Number: $decimalFormat.format($obj.intNum)

And here is how the timestamp is formatted to human readable date.下面是如何将时间戳格式化为人类可读的日期。

DateFormat DATETIME_FORMAT = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
velocityContext.put("datetimeFormat", DATETIME_FORMAT);

Timestamp to Date: $datetimeFormat.format($obj.timestamp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM