View Single Post
Old 07-31-2025, 05:44 PM   #6
Far_Pen_6884
Enthusiast
Far_Pen_6884 began at the beginning.
 
Posts: 27
Karma: 10
Join Date: Jul 2025
Device: paperwhite
To display word counts in Calibre using "K" for thousands and "M" for millions (like 1,500 as 1.5K, 34,267 as 34.3K, and 1,234,567 as 1.2M), you can leverage Calibre’s template language and its built-in functions.

### Recommended Approach: `human_readable()` Function

Calibre provides a built-in template function called `human_readable()` that takes a number and returns a string with an appropriate suffix (KB, MB, GB, etc.). You can use the `substr()` function to remove the 'B' at the end.

#### How to Set It Up

1. **Create a Custom Column** for your word count (if not already present), for example, `#wordcount`.
2. **Add a “Column built from other columns”** (a composite/custom column).
3. **Use this template in the column’s template field:**

```plaintext
program: substr(human_readable(field('#wordcount')), 0, -1)
```

- This will display:
- 1,234 → 1.2K
- 12,345 → 12.3K
- 1,234,567 → 1.2M

#### Explanation

- `field('#wordcount')` pulls the value from your custom word count column.
- `human_readable(...)` converts the number to a compact format using "KB" for thousand, "MB" for million, and so on.
- `substr(..., 0, -1)` removes the last character ('B') from the resulting string.

### Alternate: Manual Formatting Using Conditional Logic

If you want to tweak the display further or the built-in function doesn’t behave exactly how you prefer, you can use `switch_if()` to implement your own logic:

```plaintext
program:
switch_if(
field('#wordcount') >= 1000000, format_number(divide(field('#wordcount'),1000000), '{:.1f}M'),
field('#wordcount') >= 1000, format_number(divide(field('#wordcount'),1000), '{:.1f}K'),
field('#wordcount')
)
```

- Displays the count in “M” if it’s ≥1,000,000, in “K” if ≥1,000, and as the raw number otherwise.
Far_Pen_6884 is offline   Reply With Quote