Skip to content

Commit

Permalink
LDEV-5189 - improve BigDecimal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 9, 2024
1 parent e626ff6 commit f1763a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
package lucee.runtime.functions.international;

import java.lang.ref.SoftReference;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import lucee.runtime.PageContext;
import lucee.runtime.engine.ThreadLocalPageContext;
import lucee.runtime.exp.ExpressionException;
import lucee.runtime.exp.PageException;
import lucee.runtime.ext.function.Function;
Expand All @@ -41,14 +44,16 @@ public final class LSParseNumber implements Function {
private static Map<Locale, SoftReference<NumberFormat>> formatters = new ConcurrentHashMap<Locale, SoftReference<NumberFormat>>();

public static Number call(PageContext pc, String string) throws PageException {
return toNumber(pc.getLocale(), string);
if (ThreadLocalPageContext.preciseMath(pc)) return toBigDecimal(pc.getLocale(), string);
return toDouble(pc.getLocale(), string);
}

public static Number call(PageContext pc, String string, Locale locale) throws PageException {
return toNumber(locale == null ? pc.getLocale() : locale, string);
if (ThreadLocalPageContext.preciseMath(pc)) return toBigDecimal(locale == null ? pc.getLocale() : locale, string);
return toDouble(locale == null ? pc.getLocale() : locale, string);
}

public static Number toNumber(Locale locale, String str) throws PageException {
public static Number toDouble(Locale locale, String str) throws PageException {
SoftReference<NumberFormat> tmp = formatters.remove(locale);
NumberFormat nf = tmp == null ? null : tmp.get();
if (nf == null) {
Expand All @@ -64,13 +69,37 @@ public static Number toNumber(Locale locale, String str) throws PageException {
throw new ExpressionException("can't parse String [" + str + "] against locale [" + LocaleFactory.getDisplayName(locale) + "] to a number");
}
if (result == null) throw new ExpressionException("can't parse String [" + str + "] against locale [" + LocaleFactory.getDisplayName(locale) + "] to a number");
return result;
return result.doubleValue();
}
finally {
formatters.put(locale, new SoftReference<NumberFormat>(nf));
}
}

public static BigDecimal toBigDecimal(Locale locale, String str) throws PageException {
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(locale);
df.setParseBigDecimal(true);

try {
str = optimze(str.toCharArray());

ParsePosition pp = new ParsePosition(0);
BigDecimal result = (BigDecimal) df.parse(str, pp);

if (pp.getIndex() < str.length()) {
throw new ExpressionException("Can't parse String [" + str + "] against locale [" + LocaleFactory.getDisplayName(locale) + "] to a BigDecimal");
}
if (result == null) {
throw new ExpressionException("Can't parse String [" + str + "] against locale [" + LocaleFactory.getDisplayName(locale) + "] to a BigDecimal");
}

return result;
}
finally {
// No caching here, but you could implement caching if necessary
}
}

private static String optimze(char[] carr) {
StringBuilder sb = new StringBuilder();
char c;
Expand Down
4 changes: 2 additions & 2 deletions test/functions/LSParseNumber.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
var n = "2.305.843.009.213.693.951,01";
var locale = "german (standard)";
application action="update" preciseMath=false;
expect( LSParseNumber( n, locale ) ).toBe( 2305843009213693696.01 );
expect( LSParseNumber( n, locale ) ).toBe( 2305843009213693952 );
application action="update" preciseMath=true;
expect( LSparseNumber( n, locale ) ).toBe( 2305843009213693952.01 ); // LSParseNumber doesn't support big numbers
expect( LSparseNumber( n, locale ) ).toBe( 2305843009213693951.01 ); // LSParseNumber doesn't support big numbers
});

} );
Expand Down

0 comments on commit f1763a1

Please sign in to comment.