如何使用 Velocity NumberTool 处理大货币数字
How to use Velocity NumberTool for big currency numbers
我在我的项目中使用 Joda Money.class 作为货币。
我正在使用 NumberTool 来格式化 Velocity 模板中的数字。
context.put("number", new NumberTool());
我将 Money 值转换为整数,以便将这些值放入 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;
}
}
$number.format("#,##0.00", "123456789.01") // 123,456,789.01
我试过这个:
$number.format('¤#,##0.00', $numberValue)
并且工作正常
我在我的项目中使用 Joda Money.class 作为货币。
我正在使用 NumberTool 来格式化 Velocity 模板中的数字。
context.put("number", new NumberTool());
我将 Money 值转换为整数,以便将这些值放入 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;
}
}
$number.format("#,##0.00", "123456789.01") // 123,456,789.01
我试过这个:
$number.format('¤#,##0.00', $numberValue)
并且工作正常