From d244f66b44a385aead97c497d65161047616b4ea Mon Sep 17 00:00:00 2001 From: Dave Parker Date: Fri, 23 Apr 2010 13:39:37 +0000 Subject: [PATCH] Double format method matching C. git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@1847 bbc10eb1-c90d-0410-af57-cb519fbb1720 --- prism/src/prism/PrismUtils.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/prism/src/prism/PrismUtils.java b/prism/src/prism/PrismUtils.java index 94518b32..5050bc09 100644 --- a/prism/src/prism/PrismUtils.java +++ b/prism/src/prism/PrismUtils.java @@ -36,7 +36,7 @@ public class PrismUtils { // Threshold for comparison of doubles public static double epsilonDouble = 1e-12; - + /** * Compute logarithm of x to base b. */ @@ -90,7 +90,7 @@ public class PrismUtils // For two (near) zero values, return true, for just one, return false if (d1 < epsilonDouble) return (d2 < epsilonDouble); - return ( Math.abs(d1 - d2) / d1 < epsilon); + return (Math.abs(d1 - d2) / d1 < epsilon); } /** @@ -180,8 +180,18 @@ public class PrismUtils private static DecimalFormat formatterDouble2dp = new DecimalFormat("#0.00"); + /** + * Format a double, as would be done by printf's %.12g + */ + public static String formatDouble(double d) + { + // Note that we have to tweak this since Java do it quite like C. + return String.format("%.12g", d).replaceFirst("\\.?0+(e|$)", "$1"); + } + /** * Format a double. + * TODO: fix me (precision ignored, re-use above code instead) */ public static String formatDouble(int precision, double d) {