You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by ri...@apache.org on 2014/05/27 10:35:20 UTC
[7/9] git commit: more tests and minor fixes for double-string
methods, as per code review
more tests and minor fixes for double-string methods, as per code review
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/7a6827eb
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/7a6827eb
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/7a6827eb
Branch: refs/heads/master
Commit: 7a6827ebf7e8a182d0fec66e409e13c7bea51618
Parents: 99b9e19
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Authored: Mon May 26 15:10:25 2014 +0100
Committer: Alex Heneveld <al...@cloudsoftcorp.com>
Committed: Mon May 26 15:10:25 2014 +0100
----------------------------------------------------------------------
.../src/main/java/brooklyn/util/text/Strings.java | 4 +++-
.../brooklyn/util/math/MathFunctionsTest.java | 6 ++++++
.../test/java/brooklyn/util/text/StringsTest.java | 18 ++++++++++++++++++
3 files changed, 27 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7a6827eb/utils/common/src/main/java/brooklyn/util/text/Strings.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/text/Strings.java b/utils/common/src/main/java/brooklyn/util/text/Strings.java
index 7d024a6..c74c450 100644
--- a/utils/common/src/main/java/brooklyn/util/text/Strings.java
+++ b/utils/common/src/main/java/brooklyn/util/text/Strings.java
@@ -328,10 +328,12 @@ public class Strings {
* @param skipDecimalThreshhold if positive it will not add a decimal part if the fractional part is less than this threshhold
* (but for a value 3.00001 it would show zeroes, e.g. with 3 precision and positive threshhold <= 0.00001 it would show 3.00);
* if zero or negative then decimal digits are always shown
- * @param useEForSmallNumbers whether to use E notation for numbers near zero
+ * @param useEForSmallNumbers whether to use E notation for numbers near zero (e.g. 0.001)
* @return such a string
*/
public static String makeRealString(double x, int maxlen, int prec, int leftPadLen, double skipDecimalThreshhold, boolean useEForSmallNumbers) {
+ if (x<0) return
+ "-"+makeRealString(-x, maxlen, prec, leftPadLen);
NumberFormat df = DecimalFormat.getInstance();
//df.setMaximumFractionDigits(maxlen);
df.setMinimumFractionDigits(0);
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7a6827eb/utils/common/src/test/java/brooklyn/util/math/MathFunctionsTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/brooklyn/util/math/MathFunctionsTest.java b/utils/common/src/test/java/brooklyn/util/math/MathFunctionsTest.java
index 6f0955d..05cf12c 100644
--- a/utils/common/src/test/java/brooklyn/util/math/MathFunctionsTest.java
+++ b/utils/common/src/test/java/brooklyn/util/math/MathFunctionsTest.java
@@ -12,7 +12,13 @@ public class MathFunctionsTest {
}
@Test
+ public void testReadableString() {
+ Assert.assertEquals(MathFunctions.readableString(3, 5).apply(0.0123456), "1.23E-2");
+ }
+
+ @Test
public void testPercent() {
Assert.assertEquals(MathFunctions.percent(3).apply(0.0123456), "1.23%");
}
+
}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7a6827eb/utils/common/src/test/java/brooklyn/util/text/StringsTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/brooklyn/util/text/StringsTest.java b/utils/common/src/test/java/brooklyn/util/text/StringsTest.java
index b17befa..9f9b63b 100644
--- a/utils/common/src/test/java/brooklyn/util/text/StringsTest.java
+++ b/utils/common/src/test/java/brooklyn/util/text/StringsTest.java
@@ -290,5 +290,23 @@ public class StringsTest {
Assert.assertEquals(Strings.getWordCount("hello world \nit's me!\n", true), 3);
Assert.assertEquals(Strings.getWordCount("hello world \nit's me!\n", false), 4);
}
+
+ @Test
+ public void testMakeRealString() {
+ // less precision = less length
+ Assert.assertEquals(Strings.makeRealString(1.23456d, 4, 2, 0), "1.2");
+ // precision trumps length, and rounds
+ Assert.assertEquals(Strings.makeRealString(1.23456d, 4, 5, 0), "1.2346");
+ // uses E notation when needed
+ Assert.assertEquals(Strings.makeRealString(123456, 2, 2, 0), "1.2E5");
+ // and works with negatives
+ Assert.assertEquals(Strings.makeRealString(-123456, 2, 2, 0), "-1.2E5");
+ // and very small negatives
+ Assert.assertEquals(Strings.makeRealString(-0.000000000123456, 2, 2, 0), "-1.2E-10");
+ // and 0
+ Assert.assertEquals(Strings.makeRealString(0.0d, 4, 2, 0), "0");
+ // skips E notation and gives extra precision when it's free
+ Assert.assertEquals(Strings.makeRealString(123456, 8, 2, 0), "123456");
+ }
}