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

发布于 2024-12-26 07:32:53 字数 437 浏览 3 评论 0原文

我在速度模板中得到一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

指尖凝香 2025-01-02 07:32:53

Velocity模板中预计会使用Velocity工具;本质上,它们是添加到模板中可用变量的对象,以便您可以使用 $numberTool.format("#0.00", $val) 或类似的。如果没有可用的工具不能满足您的需求,只需创建一个 POJO 并将其添加到模板中即可。

要使其正常工作,您还应该添加以下 Maven 依赖项:

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

并编写以下代码:

context.put("numberTool", new NumberTool());

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:

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

and write following code:

context.put("numberTool", new NumberTool());
扛刀软妹 2025-01-02 07:32:53
#set($String = "abc")
$String.format("%.2f", $val)

在这种情况下,$val 必须是 Double 或 Float...

#set($String = "abc")
$String.format("%.2f", $val)

$val has to be Double or Float in this case...

简单爱 2025-01-02 07:32:53

使用 VelocityTools 项目中的 MathTool。

$math.roundTo(2, $val)

Use the MathTool from the VelocityTools project.

$math.roundTo(2, $val)
罗罗贝儿 2025-01-02 07:32:53

格式货币($值)。这是很好的 Java 速度代码,用于将数字格式化为货币格式。

formatCurrency($value). This is good java velocity code to format a number to currency format.

始终不够爱げ你 2025-01-02 07:32:53

仅使用 Java 格式化程序的解决方案:(无需额外的库)

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

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

以下是如何将时间戳格式化为人类可读的日期。

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

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

Solution by just using the Java Formatters: (without additional libraries)

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)
伪装你 2025-01-02 07:32:53

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

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

橘寄 2025-01-02 07:32:53

除了使用 $numberTool.format 之外,更好的方法是使用基于 MessageFormat 的工具类,它不仅仅可以执行数字操作。例如,我们使用 Struts 特定的 MessageTool,但您可以使用类似 ResourceTool 的东西来代替:

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

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

这样,您就可以在上下文中获取数字,而不仅仅是通过以下方式获取数字 :本身。在非英语语言中,数字可能更适合位于文本的左侧、中间或其他位置。与简单地单独格式化数字相比,这为您提供了更大的灵活性。

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. For example, we use MessageTool which is Struts-specific, but you can use something similar like ResourceTool instead:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文