Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RoundingMode#FLOOR for percentages in HTML report #452

Merged
merged 1 commit into from
Sep 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ <h3>New Features</h3>

<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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public void testItem2() throws Exception {
support.findStr(doc, "/html/body/table/tr/td[1]/text()"));
}

@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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -79,10 +80,21 @@ private void cell(final HTMLElement td, final ICoverageNode node)
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;
}
Expand Down