aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--org.jacoco.doc/docroot/doc/changes.html4
-rw-r--r--org.jacoco.report.test/src/org/jacoco/report/internal/html/table/PercentageColumnTest.java10
-rw-r--r--org.jacoco.report/src/org/jacoco/report/internal/html/table/PercentageColumn.java18
3 files changed, 29 insertions, 3 deletions
diff --git a/org.jacoco.doc/docroot/doc/changes.html b/org.jacoco.doc/docroot/doc/changes.html
index 51a58ed3..0ee9e676 100644
--- a/org.jacoco.doc/docroot/doc/changes.html
+++ b/org.jacoco.doc/docroot/doc/changes.html
@@ -29,6 +29,10 @@
<h3>Fixed Bugs</h3>
<ul>
+ <li>Use <code>RoundingMode#FLOOR</code> instead of
+ <code>RoundingMode#HALF_EVEN</code> for percentages in HTML report, so that
+ "99.5" is displayed as "99%", not as "100%"
+ (GitHub <a href="https://github.com/jacoco/jacoco/issues/452">#452</a>).</li>
<li>Do not add useless members into Java 8 interfaces that have only interface
initialization and abstract methods
(GitHub <a href="https://github.com/jacoco/jacoco/issues/441">#441</a>).</li>
diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/PercentageColumnTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/PercentageColumnTest.java
index fb3cad09..6b472056 100644
--- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/PercentageColumnTest.java
+++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/PercentageColumnTest.java
@@ -98,6 +98,16 @@ public class PercentageColumnTest {
}
@Test
+ public void testRounding() throws Exception {
+ final ITableItem item = createItem(1, 199);
+ column.item(td, item, resources, root);
+ doc.close();
+ final Document doc = support.parse(output.getFile("Test.html"));
+ assertEquals("99%",
+ support.findStr(doc, "/html/body/table/tr/td[1]/text()"));
+ }
+
+ @Test
public void testLocale() throws Exception {
IColumnRenderer column = new PercentageColumn(CounterEntity.LINE,
Locale.FRENCH);
diff --git a/org.jacoco.report/src/org/jacoco/report/internal/html/table/PercentageColumn.java b/org.jacoco.report/src/org/jacoco/report/internal/html/table/PercentageColumn.java
index 3f9a21a8..45105fdb 100644
--- a/org.jacoco.report/src/org/jacoco/report/internal/html/table/PercentageColumn.java
+++ b/org.jacoco.report/src/org/jacoco/report/internal/html/table/PercentageColumn.java
@@ -12,7 +12,8 @@
package org.jacoco.report.internal.html.table;
import java.io.IOException;
-import java.text.DecimalFormat;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Comparator;
import java.util.List;
@@ -50,7 +51,7 @@ public class PercentageColumn implements IColumnRenderer {
*/
public PercentageColumn(final CounterEntity entity, final Locale locale) {
this.entity = entity;
- this.percentageFormat = DecimalFormat.getPercentInstance(locale);
+ this.percentageFormat = NumberFormat.getPercentInstance(locale);
comparator = new TableItemComparator(
CounterComparator.MISSEDRATIO.on(entity));
}
@@ -79,10 +80,21 @@ public class PercentageColumn implements IColumnRenderer {
if (total == 0) {
td.text("n/a");
} else {
- td.text(percentageFormat.format(counter.getCoveredRatio()));
+ td.text(format(counter.getCoveredRatio()));
}
}
+ /**
+ * Ratio 199/(1+199)=0.995 must be displayed as "99%", not as "100%".
+ * Unfortunately {@link NumberFormat} uses {@link RoundingMode#HALF_EVEN} by
+ * default and ability to change available only starting from JDK 6, so
+ * perform rounding using {@link RoundingMode#FLOOR} before formatting.
+ */
+ private String format(double ratio) {
+ return percentageFormat.format(
+ BigDecimal.valueOf(ratio).setScale(2, RoundingMode.FLOOR));
+ }
+
public Comparator<ITableItem> getComparator() {
return comparator;
}