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 a412b5a commit 153995e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions core/src/main/java/lucee/runtime/op/Caster.java
Original file line number Diff line number Diff line change
Expand Up @@ -2293,9 +2293,11 @@ public static String toString3(double d) {
return str;
}

private static DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.US);// ("#.###########");
private static DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
private static DecimalFormat ff = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
static {
df.applyLocalizedPattern("#.########################");
ff.applyLocalizedPattern("#.#######");
}

public static String toString(double d) {
Expand All @@ -2308,9 +2310,20 @@ 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(Number n) {
if (n instanceof BigDecimal) return df.format(n);
if (n instanceof Double || n instanceof Float) return Caster.toString(n.doubleValue());
if (n instanceof Double) return Caster.toString(n.doubleValue());
if (n instanceof Float) return Caster.toString(n.floatValue());
if (n instanceof Long) return Caster.toString(n.longValue());
return n.toString();
}
Expand Down

0 comments on commit 153995e

Please sign in to comment.