Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thymeleaf/thymeleaf#604 add functions for currency formatting #715

Open
wants to merge 2 commits into
base: 3.1-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.1.3
=========
- Added `Numbers` functions to format numbers with a chosen currency.



3.1.2
=====
- Allow java.sql.* types in expressions.
Expand Down
49 changes: 47 additions & 2 deletions lib/thymeleaf/src/main/java/org/thymeleaf/expression/Numbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.thymeleaf.expression;

import java.util.ArrayList;
import java.util.Currency;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -344,6 +345,17 @@ public String formatCurrency(final Number target) {
throw new TemplateProcessingException("Error formatting currency", e);
}
}

public String formatCurrency(final Number target, final Currency currency) {
if (target == null) {
return null;
}
try {
return NumberUtils.formatCurrency(target, this.locale, currency);
} catch (final Exception e) {
throw new TemplateProcessingException("Error formatting currency", e);
}
}

public String[] arrayFormatCurrency(final Object[] target) {
if (target == null) {
Expand All @@ -355,7 +367,18 @@ public String[] arrayFormatCurrency(final Object[] target) {
}
return result;
}


public String[] arrayFormatCurrency(final Object[] target, final Currency currency) {
if(target == null) {
return null;
}
final String[] result = new String[target.length];
for (int i = 0; i < target.length; i++) {
result[i] = formatCurrency((Number) target[i], currency);
}
return result;
}

public List<String> listFormatCurrency(final List<? extends Number> target) {
if (target == null) {
return null;
Expand All @@ -366,7 +389,18 @@ public List<String> listFormatCurrency(final List<? extends Number> target) {
}
return result;
}


public List<String> listFormatCurrency(final List<? extends Number> target, final Currency currency) {
if (target == null) {
return null;
}
final List<String> result = new ArrayList<String>(target.size() + 2);
for (final Number element : target) {
result.add(formatCurrency(element, currency));
}
return result;
}

public Set<String> setFormatCurrency(final Set<? extends Number> target) {
if (target == null) {
return null;
Expand All @@ -378,6 +412,17 @@ public Set<String> setFormatCurrency(final Set<? extends Number> target) {
return result;
}

public Set<String> setFormatCurrency(final Set<? extends Number> target, final Currency currency) {
if (target == null) {
return null;
}
final Set<String> result = new LinkedHashSet<String>(target.size() + 2);
for (final Number element : target) {
result.add(formatCurrency(element, currency));
}
return result;
}




Expand Down
14 changes: 14 additions & 0 deletions lib/thymeleaf/src/main/java/org/thymeleaf/util/NumberUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Currency;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -287,6 +288,19 @@ public static String formatCurrency(final Number target, final Locale locale) {
return format.format(target);
}

public static String formatCurrency(final Number target, final Locale locale, final Currency currency) {
Validate.notNull(locale, "Locale cannot be null");
if(target == null) {
return null;
}

NumberFormat format = NumberFormat.getCurrencyInstance(locale);
format.setCurrency(currency);
format.setMaximumFractionDigits(currency.getDefaultFractionDigits());

return format.format(target);
}

/**
* Formats a number as a percentage value.
*
Expand Down