Skip to content

Commit 47f2823

Browse files
committed
Optimise PrismUtils.formatDouble (used heavily for model exports).
1 parent dfca98f commit 47f2823

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

prism/src/prism/PrismUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,17 @@ public static String formatDouble(int prec, double d)
573573
// strip trailing zeros after the .
574574
String result = String.format((Locale)null, "%." + prec + "g", d);
575575
// if there are only zeros after the . (e.g., .000000), strip them including the .
576-
result = result.replaceFirst("\\.0+(e|$)", "$1");
576+
result = FORMAT_DOUBLE_TRAILING_ZEROS.matcher(result).replaceFirst("$1");
577577
// handle .xxxx0000
578578
// we first match .xxx until there are only zeros before the end (or e)
579579
// as we match reluctantly (using the *?), all trailing zeros are captured
580580
// by the 0+ part
581-
return result.replaceFirst("(\\.[0-9]*?)0+(e|$)", "$1$2");
581+
return FORMAT_DOUBLE_TRAILING_ZEROS2.matcher(result).replaceFirst("$1$2");
582582
}
583583

584+
private static final Pattern FORMAT_DOUBLE_TRAILING_ZEROS = Pattern.compile("\\.0+(e|$)");
585+
private static final Pattern FORMAT_DOUBLE_TRAILING_ZEROS2 = Pattern.compile("(\\.[0-9]*?)0+(e|$)");
586+
584587
/**
585588
* Format a double (that is known to be an integer, but not necessarily in the int range)
586589
* to a string.<br>

0 commit comments

Comments
 (0)