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 Apr 3, 2021
1 parent 309b9f2 commit 5dacfbd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Added "Automatic-Module-Name: thymeleaf" to MANIFEST.MF for Java 9+ JPMS.
- Updated AttoParser dependency to 2.0.5.RELEASE
- Updated Unbescape dependency to 1.1.6.RELEASE
- Added `Numbers` functions to format numbers with a chosen currency.


3.0.9
Expand Down
49 changes: 47 additions & 2 deletions 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 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 5dacfbd

Please sign in to comment.