Skip to content

Commit

Permalink
LDEV-4780 - handle float separate in Caster
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Jan 12, 2024
1 parent 47718e7 commit 02e8cb5
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion core/src/main/java/lucee/runtime/op/Caster.java
Original file line number Diff line number Diff line change
Expand Up @@ -2140,9 +2140,11 @@ public static String toString3(double d) {
return str;
}

private static DecimalFormat ff = (DecimalFormat) DecimalFormat.getInstance(Locale.US);// ("#.###########");
private static DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.US);// ("#.###########");
private static DecimalFormat dfp = (DecimalFormat) DecimalFormat.getInstance(Locale.US);// ("#.###########");
static {
ff.applyLocalizedPattern("#.#######");
df.applyLocalizedPattern("#.############");
dfp.applyLocalizedPattern("#.###############");
}
Expand All @@ -2157,6 +2159,16 @@ public static String toString(double d) {
return df.format(d);
}

public static String toString(float f) {
long l = (long) f;
if (l == f) return toString(l);

if (f > l && (f - l) < 0.000000000001) return toString(l);
if (l > f && (l - f) < 0.000000000001) return toString(l);

return ff.format(f);
}

public static String toString(BigDecimal bd) {
return df.format(bd);
}
Expand All @@ -2170,7 +2182,8 @@ public static String toString(Number n) {
if (d > l && (d - l) < 0.000000000001) return toString(l);
if (l > d && (l - d) < 0.000000000001) return toString(l);

if (n instanceof Double || n instanceof Float) return toString(n.doubleValue());
if (n instanceof Double) return toString(n.doubleValue());
if (n instanceof Float) return toString(n.floatValue());
return n.toString();
// return df.format(d);
}
Expand Down

0 comments on commit 02e8cb5

Please sign in to comment.