How to use Velocity NumberTool for big currency numbers


I'm using the Joda Money.class for currency in my project.<br /> I'm using the NumberTool for formatting numbers inside a Velocity template.

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

I convert Money values into ints for placing these values inside a Velocity template.


// 123435 €1234.35 priceInCents = price.getAmountMinorInt();

I want to format this value inside the Velocity template as below:


How do I accomplish this?

I tried the following cases:

$number.format('#0,00', $price) //Output: 1234,35 $number.format('#0.00', $price) //Output: 123435.00 $number.format('#00,00', $price) //Output: 12,34,35 $number.format('#00.00', $price) //Output: 123435.00 $number.format('#000,00', $price) //Output: 12,34,35 $number.format('#000.00', $price) //Output: 123435.00


So I found my answer. <strong>Make your own tool</strong>

context.put("currency", new CurrencyTool());



Basics created tool:

/** * Tool for doing currency formatting */ @DefaultKey(value = "currency") public final class CurrencyTool extends FormatConfig { /** * Constructor */ public CurrencyTool() { } /** * @param money The {@link Money} object to format * @return */ public String format(Money money) { // Get the currency as an integer final int value = money.getAmountMinorInt(); // Determine the units and cents final int cents = value % 100; String units = "" + (value / 100); // Final formatted values String formattedCents = ""; String formattedUnits = ""; // Format the cents to a 2 decimal number formattedCents = cents < 10 ? "0" + cents : "" + cents; // Format the units to parts of 3 separated with a dot if (units.length() > 3) { // units is above 999, formatting required //Determine the length of the part that doesn't needs to be segmented //example: 12 of 12345234 (12.345.234) final int nonSegment = units.length() % 3; formattedUnits = units.substring(0, nonSegment); //place a dot for each segment //example: nonSegment (dot) 345 (dot) 234 units = units.substring(nonSegment, units.length()); for (String segment : units.split("(?<=\\G...)")) { formattedUnits = formattedUnits + "." + segment; } } else { // units is below 1000, no formatting required formattedUnits = units; } return formattedUnits + "," + formattedCents; } }


$number.format("#,##0.00", "123456789.01") // 123,456,789.01


That doesn't work for me:

$number.format("#,##0.00", "123456789.01") // Invocation of method 'format' in class org.apache.velocity.tools.generic.NumberTool threw exception java.lang.IllegalArgumentException: Malformed pattern "#,"

Changing to this appears to work:

$number.format("##,##0.00", "123456789.01") // 123,456,789.01

However it fails in this scenario:

$number.format("##,##0.00", "123456789.10") // 123,456,789.1 <-- missing trailing 0

Seems broken to me...


I have tried with this:

$number.format('¤#,##0.00', $numberValue)

and works ok


  • How to change a text field to a date field in SQL Select Statement
  • How to convert a float column into a datetime
  • How to create a regex that will match regardless of the sequence of characters?
  • I want to use WebBrowser Control in Mono Windows Form
  • More efficient rolling join backwards not forwards?
  • Send command to FTP server using sockets in C
  • Javascript project with multiple files
  • How to open Theme editor in android studio
  • Globalized custom number formatting - Variable decimal points
  • Copying different structs to byte arrays
  • Inotify linux watching subdirectories
  • insert into a MySQL database on a different server
  • MVC 3 - Editor template for List Model [closed]
  • How to compare source in Git repository between source in SVN repository
  • Helgrind for Windows?
  • JHipster Entity Generator crahing
  • ActiveAdmin “f.inputs, except: [:field]” doesn't work
  • How to check Eclipse plugin performance
  • Delete multiple lines - from “patternA” match, through second occurrence of “patternB”
  • Sinatra server won't start - “wrong number of arguments”
  • How can i automate 'Settings' app in real iOS devices?
  • How to insert an Image in WORD after a bookmark using OpenXML
  • Eclipse: missing requirement E4 RCP patch when installing properties editor
  • Find symbol dependencies in source code [closed]
  • Why is it still possible to insert a foreign key that doesn't exist?
  • How to run bash commands like “npm install” on complie
  • UML diagram generator in Visual Studio 2010
  • Pythons argparse default value doesn't work
  • Detection of framework usage on Mac system?
  • using html data-attributes as css-variable (i.e. text-shadow)
  • Click on button in another program - FindWindow, C#
  • VSCode change debug shell to bash on windows
  • Does Apple allow the usage of sysctl.h within iOS applications?
  • Memory error in python- how to use more memory
  • Not able to aggregate on nested fields in elasticsearch
  • Sort List of Strings By Version
  • Moving mysql files across servers
  • Incrementing object id automatically JS constructor (static method and variable)
  • Free memory of cv::Mat loaded using FileStorage API
  • unknown Exception android