如何使用Velocity的NumberTool大货币数量 [英] How to use Velocity NumberTool for big currency numbers

查看:435
本文介绍了如何使用Velocity的NumberTool大货币数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用Joda Money.class作为货币。

我使用NumberTool格式化Velocity模板中的数字。

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

我将Money值转换为整数,以便将这些值放入Velocity模板中。
$ b

例子:

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

我想在Velocity模板中格式化这个值,如下所示:

  1.234,35 



我试过以下情况: number.format('#0,00',$ price)//输出:1234,35
$ number.format('#0.00',$ price)//输出:123435.00
$ number。格式('#00,00',$ price)//输出:12,34,35
$ number.format('#00.00',$ price)//输出:123435.00
$数字。格式('#000,00',$ price)//输出:12,34,35
$ number.format('#000.00',$ price)//输出:123435.00


解决方案

所以我找到了我的答案。
制作您自己的工具

  context.put(currency,new CurrencyTool )); 

用法:

  $ currency.format($ price)

基本创建工具:

  / ** 
*进行货币格式化的工具
* /
@DefaultKey(value =currency )
public final class CurrencyTool extends FormatConfig {
$ b / **
*构造函数
* /
public CurrencyTool(){
}
$ b / **
* @param money用于格式化
* @return
* /
的{@link Money对象public String format(Money钱){
//获取货币作为整数
final int value = money.getAmountMinorInt();

//确定单位和美分
final int cents = value%100;
String units =+(value / 100);

//最终格式化的值
String formattedCents =;
String formattedUnits =;

//将美分格式化为2位十进制数
formattedCents =美分< 10? 0+分:+分;

//将单位格式化为以点分隔的部分3
if(units.length()> 3){
//单位高于999,需要格式

//确定不需要分割的零件的长度
//示例:12 12345234(12.345.234)
final int nonSegment =单位。长度()%3;
formattedUnits = units.substring(0,nonSegment);

//为每个段放置一个点
//例如:nonSegment(点)345(点)234
units = units.substring(nonSegment,units.length() );
for(String segment:units.split((?< = \\G ...))){
formattedUnits = formattedUnits +。 +细分;
}
} else {
//单位低于1000,不需要格式
formattedUnits = units;
}

返回formattedUnits +,+ formattedCents;
}
}


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

解决方案

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

这篇关于如何使用Velocity的NumberTool大货币数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆