{
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--;
+ }
+ }
+ }
}
--- /dev/null
+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");
+ }
+}