简体   繁体   中英

Formatting a BigDecimal in Apache Velocity

In my velocity templates, I've been using the Velocity NumberTool to format number variables as a currency. Most of these are primitive doubles and they work fine with this tool.

However, some are of type BigDecimal and they don't seem to be formatted at all. It'll just show the expression as is when rendered:

$global.numberTool.currency($someBigDecimalVariable)

Is there a convenient way to format a BigDecimal with Velocity as a currency? What are the alternatives?

Since you can put any object into a Velocity context, the easiest thing to do is to put your own DecimalFormat object in your context and call its format() method in the template.

Of course this isn't a very nice solution but you can refine it to make it more generic.

Can you use JSTL in Velocity? I used the fmt JSTL Tags for displaying BigDecimals as a Currency like this:

<fmt:formatNumber value="${myBigDecimalValue}" type="currency" currencySymbol="€" currencyCode="EUR" minFractionDigits="2" maxFractionDigits="2"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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