Java 如何使用Velocity NumberTool处理大货币数字

Java 如何使用Velocity NumberTool处理大货币数字,java,formatting,velocity,Java,Formatting,Velocity,我在项目中使用Joda Money.class作为货币。 我使用NumberTool在Velocity模板中格式化数字 context.put("number", new NumberTool()); 我将货币值转换为整数,以便将这些值放入Velocity模板中 例如: // 123435 €1234.35 priceInCents = price.getAmountMinorInt(); 我想在Velocity模板中设置此值的格式,如下所示: 1.234,35 我如何做

我在项目中使用Joda Money.class作为货币。
我使用NumberTool在Velocity模板中格式化数字

context.put("number", new NumberTool()); 
我将货币值转换为整数,以便将这些值放入Velocity模板中

例如:

// 123435         €1234.35
priceInCents = price.getAmountMinorInt();
我想在Velocity模板中设置此值的格式,如下所示:

1.234,35
我如何做到这一点

我审理了下列案件:

$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
所以我找到了答案。 制作自己的工具

    context.put("currency", new CurrencyTool());
用法:

$currency.format($price)
创建的基本工具:

/**
 * 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;
    }
}
/**
*用于进行货币格式化的工具
*/
@DefaultKey(value=“currency”)
公共最终类CurrencyTool扩展FormatConfig{
/**
*建造师
*/
公共货币工具(){
}
/**
*@param money要格式化的{@link money}对象
*@返回
*/
公共字符串格式(货币){
//将货币作为整数获取
final int value=money.getAmountMinoPrint();
//确定单位和美分
最终整数=值%100;
字符串单位=“+”(值/100);
//最终格式化值
字符串formattedCents=“”;
字符串formattedUnits=“”;
//将美分格式化为2十进制数字
格式化美分=美分<10?“0”+美分:“+美分;
//将单位格式化为以点分隔的3部分
如果(单位长度()>3){
//单位大于999,需要格式化
//确定不需要分段的零件的长度
//示例:12345234中的12(12.345.234)
final int nonSegment=units.length()%3;
formattedUnits=单位。子字符串(0,非分段);
//为每个段放置一个点
//示例:非分段(dot)345(dot)234
units=units.substring(非分段,units.length());
对于(字符串段:units.split((?),我找到了答案。
制作自己的工具

    context.put("currency", new CurrencyTool());
用法:

$currency.format($price)
创建的基本工具:

/**
 * 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;
    }
}
/**
*用于进行货币格式化的工具
*/
@DefaultKey(value=“currency”)
公共最终类CurrencyTool扩展FormatConfig{
/**
*建造师
*/
公共货币工具(){
}
/**
*@param money要格式化的{@link money}对象
*@返回
*/
公共字符串格式(货币){
//将货币作为整数获取
final int value=money.getAmountMinoPrint();
//确定单位和美分
最终整数=值%100;
字符串单位=“+”(值/100);
//最终格式化值
字符串formattedCents=“”;
字符串formattedUnits=“”;
//将美分格式化为2十进制数字
格式化美分=美分<10?“0”+美分:“+美分;
//将单位格式化为以点分隔的3部分
如果(单位长度()>3){
//单位大于999,需要格式化
//确定不需要分段的零件的长度
//示例:12345234中的12(12.345.234)
final int nonSegment=units.length()%3;
formattedUnits=单位。子字符串(0,非分段);
//为每个段放置一个点
//示例:非分段(dot)345(dot)234
units=units.substring(非分段,units.length());

对于(字符串段:units.split((?我已经尝试过了:

$number.format(“·0.00”,$numberValue)


工作正常

我试过这个:

$number.format(“·0.00”,$numberValue)

工作正常