How to use Velocity NumberTool for big currency numbers

血红的双手。 提交于 2019-12-14 03:14:38

问题


I'm using the Joda Money.class for currency in my project.
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.

example:

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

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

1.234,35

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

回答1:


So I found my answer. Make your own tool

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

Usage:

$currency.format($price)

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;
    }
}



回答2:


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



回答3:


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...




回答4:


I have tried with this:

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

and works ok



来源:https://stackoverflow.com/questions/28339988/how-to-use-velocity-numbertool-for-big-currency-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!