PDFPipe

Numbers, tables and totals / Percentage column

A percentage column that reads as percentages, not as amounts

A column of rates, shares or variances, sitting next to columns of money and needing to be distinguishable from them at a glance.

Why this block is harder than it looks

A percentage in a table of currency amounts is read as an amount unless something stops it, and the thing people reach for is a percent sign on every row, which turns a clean column into a ragged one because the sign moves with the digits. Percentages also have a sign problem of their own: a variance can be negative, and if the minus sits inside the right-aligned block every negative row shifts. And a mix of whole numbers and decimals in one percentage column is common and looks like inconsistent data.

The decisions that make it work

Reasons rather than a description of the code. Each of these is a choice that has a wrong answer, and the wrong answer is usually the default.

  • Put the percent sign in the column header and omit it from the rows, so the column edge stays straight and the meaning is still stated once.
  • Where the sign has to stay on the row, give it a fixed-width slot so it does not move with the digit count.
  • Fix the decimal places for the whole column. One place or none, chosen once, is better than a mixture that is each individually correct.
  • Set the percentage column narrower than the money columns and give it a slightly lighter colour, so the eye separates the two kinds of number without a rule between them.
  • Handle negatives with a fixed-width sign slot, the same as a running balance, and never with colour alone.
  • Keep percentages and their basis adjacent. A share with no base amount next to it cannot be checked and will be queried.

The fragment

Markup and the CSS it needs, and nothing else. It expects a document that already has a page rule and a base stylesheet, so paste it into one rather than opening it on its own.

html
<table class="shares">
  <thead>
    <tr><th>Department</th><th class="n">Amount</th><th class="p">Share %</th>
        <th class="p">Change %</th></tr>
  </thead>
  <tbody>
    <tr><td>Operations</td><td class="n">184,200.00</td>
        <td class="p">41.2</td><td class="p"><i></i>3.4</td></tr>
    <tr><td>Support</td><td class="n">62,800.00</td>
        <td class="p">14.0</td><td class="p"><i>&#8722;</i>1.1</td></tr>
  </tbody>
</table>

<style>
  .shares { width: 100%; border-collapse: collapse; font-size: 9pt; table-layout: fixed; }
  .shares th, .shares td { padding: 3pt 4pt; }
  .shares .n {
    width: 22%;
    text-align: right;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
  }
  .shares .p {
    width: 14%;                        /* narrower than the money column */
    text-align: right;
    white-space: nowrap;
    color: #555;                       /* a shade back, so the eye separates them */
    font-variant-numeric: tabular-nums;
  }
  .shares .p i {
    display: inline-block;
    width: 6pt;                        /* fixed sign slot */
    font-style: normal;
  }
</style>

What breaks when it is built the obvious way

Repeating the percent sign on every row. It is the obvious way to label the column and it makes the column ragged, because the sign travels with the last digit and the digit counts differ. The header carries the label perfectly well.

How to prove it survived pagination

Render a column mixing single-digit, double-digit and negative percentages. The right edge should be straight and the decimal points should be in one vertical line. Then ask somebody which column is money and which is a percentage, without telling them.

Where it sits in a finished document

This page is one block. The document it belongs to has a page rule, a base stylesheet, a header and everything else around it, and repeating all of that here would make thirty pages that say the same thing. The multi-page report template is a complete file with this block already in it, so take that and change the fragment rather than assembling one from parts. The property doing most of the work here is covered on its own page, with the support caveats that belong there rather than here.

Frequently asked

Will this fragment work on its own?

Not as a whole document. It has no page rule, no margins and no base type, because those belong to the document rather than to the block, and duplicating them in every fragment would mean thirty copies to keep in step. Paste it into a template that already has them.

Why does it look right in a browser and wrong in the PDF?

Because a browser window is one continuous surface and a document is a stack of fixed rectangles. Nothing in a scrolling view exercises a page boundary, so every break rule in the fragment is inert until the content is paginated. Render the real thing with enough content to cross two or three boundaries, then look.

Do I need a special option on the render request for this?

No. Everything on this page is CSS and markup, which is your side of the boundary. What the render has to give you is a real page size with real margins, and after that the layout is decided by the stylesheet.

Other parts of a document

The blocks that sit next to this one, and one from the next group along so you are not sealed inside a single kind of problem.

Paste the fragment into the playground inside a page rule and see what it does at a real page size. That is the only way any of this gets confirmed.