Skip to content

Commit

Permalink
thymeleaf#604 add functions for currency formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fibsifan committed Aug 25, 2023
1 parent 4781ca8 commit 63de324
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
8 changes: 6 additions & 2 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 Expand Up @@ -170,8 +176,6 @@
output buffers when processing of the template has completely finished and output is rendered in memory.
- Added "Automatic-Module-Name: thymeleaf-spring5" to MANIFEST.MF for Java 9+ JPMS.



3.0.9
=====
- Fixed hit ratio in StandardCache not being correctly computed (always 1 or 0).
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

0 comments on commit 63de324

Please sign in to comment.