简体   繁体   中英

How can I use a custom number format with velocity's NumberTool without generating an error?

I want to use a custom currency format with Velocity's NumberTool but I'm getting an error with a valid format. Here's my code:

... $numberTool.format("¤###,###", ${amount}) ...

This generates an org.apache.velocity.exception.ParseErrorException

What is happening here is that Velocity is attempting parse the number format and getting confused. Here are two solutions:

  • Use a variable to define your format and put it in the model before hand. Then just refer to it from your template. For example, in your Java file:

     model.put("DEFAULT_CURRENCY_FORMAT", "¤###,###"); 

    and in your template:

     $number.format(${DEFAULT_CURRENCY_FORMAT}, ${amount}) 
  • Or you can simply use a different delimiter to specify your format. Even though the documentation for NumberTool.currency suggests that you use double quotes when you use NumberTool.format, you can use single quotes instead:

     $number.format('¤###,###', ${amount}) 

I prefer the first option, because you'll likely use the same format in multiple places and because it will allow you to use the ' character in your format should you need it.

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