From 1e5b6995c96364c79329e618f7933caf177b353e Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 3 Oct 2016 11:07:12 +0100 Subject: [PATCH] JAL-98 bespoke fast percentage formatter for consensus tooltip --- src/jalview/util/Format.java | 25 +++++++++++++++++++++++++ test/jalview/util/FormatTest.java | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 test/jalview/util/FormatTest.java diff --git a/src/jalview/util/Format.java b/src/jalview/util/Format.java index d14e4ad..7121985 100755 --- a/src/jalview/util/Format.java +++ b/src/jalview/util/Format.java @@ -947,4 +947,29 @@ public class Format { return formatString; } + + /** + * Bespoke method to format percentage float value to the specified number of + * decimal places. Avoids use of general-purpose format parsers as a + * processing hotspot. + * + * @param sb + * @param value + * @param dp + */ + public static void appendPercentage(StringBuilder sb, float value, int dp) + { + sb.append((int) value); + if (dp > 0) + { + sb.append("."); + while (dp > 0) + { + value = value - (int) value; + value *= 10; + sb.append((int) value); + dp--; + } + } + } } diff --git a/test/jalview/util/FormatTest.java b/test/jalview/util/FormatTest.java new file mode 100644 index 0000000..18199f9 --- /dev/null +++ b/test/jalview/util/FormatTest.java @@ -0,0 +1,32 @@ +package jalview.util; + +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +public class FormatTest +{ + @Test(groups = "Functional") + public void testAppendPercentage() + { + StringBuilder sb = new StringBuilder(); + Format.appendPercentage(sb, 123.456f, 0); + assertEquals(sb.toString(), "123"); + + sb.setLength(0); + Format.appendPercentage(sb, 123.456f, 1); + assertEquals(sb.toString(), "123.4"); + + sb.setLength(0); + Format.appendPercentage(sb, 123.456f, 2); + assertEquals(sb.toString(), "123.45"); + + sb.setLength(0); + Format.appendPercentage(sb, 123.456f, 3); + assertEquals(sb.toString(), "123.456"); + + sb.setLength(0); + Format.appendPercentage(sb, 123.456f, 4); + assertEquals(sb.toString(), "123.4560"); + } +} -- 1.7.10.2