From 153995e1044a2de7a2d5203d45edda771371554f Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Fri, 12 Jan 2024 10:51:03 +0100 Subject: [PATCH] LDEV-4780 - handle float separate in Caster --- core/src/main/java/lucee/runtime/op/Caster.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/lucee/runtime/op/Caster.java b/core/src/main/java/lucee/runtime/op/Caster.java index 7cca9f50f0..b9bdcee2c2 100755 --- a/core/src/main/java/lucee/runtime/op/Caster.java +++ b/core/src/main/java/lucee/runtime/op/Caster.java @@ -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) { @@ -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(); }