Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 07-31-2025, 12:14 PM   #1
Far_Pen_6884
Member
Far_Pen_6884 began at the beginning.
 
Posts: 10
Karma: 10
Join Date: Jul 2025
Device: paperwhite
Question How to Display Word Counts in Thousands and Millions in Calibre?

Hey everyone,

I'm trying to display word counts from FanFicFare in Calibre with both "K" for thousands and "M" for millions (e.g., 1500 as 1.5K, 34267 as 34.3K, 1234567 as 1.2M). I've seen solutions for thousands, but I'm not sure how to extend this to millions. Any tips or tricks would be greatly appreciated!

Thanks in advance for any help!

Here are a couple of solutions I found for displaying word counts in thousands:

#### Reddit Solution

From [Reddit](https://www.reddit.com/r/Calibre/com...ers/mf54thv/):

1. **Create a usual integer custom column** (e.g., named `wordcount`) to store the raw word count numbers filled by FanFicFare.
2. **Don't display this raw word count column** directly.
3. **Create a second custom column of type "Column built from other columns"** to format the display.

Use this composite template to show the word count simplified as thousands with one decimal and a "K" appended:

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

Explanation:
- `field('#wordcount')` reads the value from the column named `wordcount`.
- `divide(..., 1000)` divides the value by 1000 to convert to thousands.
- `format_number(..., '{:.1f}K')` formats the number with one decimal place and then appends "K".

For better sorting by number, instead of including "K" in the formatted string, use:

```plaintext
program: format_number(divide(field('#wordcount'), 1000), '{:.1f}')
```

and in the column settings, set **"Sort/search column by" -> "Number"**. This preserves numeric sorting by thousands. You can name this column "Length" or "Word Count (K)".

#### MobileRead Solution

From [MobileRead](https://www.mobileread.com/forums/sh...php?t=340393):

A related example from MobileRead shows a simple template solution for 1.5k format using Calibre's program template syntax to do calculations manually:

```plaintext
program:
before = round($$#words/1000);
after = round(($$#words - (before*1000))/100)*0.1;
complete = before + after
```
Far_Pen_6884 is offline   Reply With Quote
Old 07-31-2025, 12:38 PM   #2
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,449
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Use switch_if() or first_matching_cmp().

Edit: the human_readable() function might do what you want

Last edited by chaley; 07-31-2025 at 12:46 PM.
chaley is offline   Reply With Quote
Advert
Old 07-31-2025, 05:02 PM   #3
Far_Pen_6884
Member
Far_Pen_6884 began at the beginning.
 
Posts: 10
Karma: 10
Join Date: Jul 2025
Device: paperwhite
https://manual.calibre-ebook.com/_mo...nHumanReadable

human_readable() function does pretty much what I want except it returns the value in KB, GB, MB, etc. I've decided to go with that until I find a better solution.
Far_Pen_6884 is offline   Reply With Quote
Old 07-31-2025, 05:13 PM   #4
Far_Pen_6884
Member
Far_Pen_6884 began at the beginning.
 
Posts: 10
Karma: 10
Join Date: Jul 2025
Device: paperwhite
https://manual.calibre-ebook.com/gen...human-readable

Maybe something like:

program: re_group(human_readable(field('#wordcount')), '([0-9.]+)([KMGTPEZY]?)', '\1\2')

or using the switch_if():

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')
)
Far_Pen_6884 is offline   Reply With Quote
Old 07-31-2025, 05:36 PM   #5
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,449
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
You could use substr() to chop off the last character.
chaley is offline   Reply With Quote
Advert
Old 07-31-2025, 05:44 PM   #6
Far_Pen_6884
Member
Far_Pen_6884 began at the beginning.
 
Posts: 10
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
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to display word count under book details? kamille3 Library Management 6 08-04-2021 02:29 PM
Rounding Word Counts in a Custom Column eosrose Library Management 0 05-18-2020 08:44 PM
Item counts, page counts and a search capability HappyChris Amazon Kindle 3 05-21-2019 02:17 PM
Creator Is it possible to display different meanings of a word in a dictionary? mycityofsky Kindle Formats 0 08-21-2014 11:19 PM
Microsoft to cut its Surface orders in half (2 millions units instead of 4 millions) Top100EbooksRank News 115 12-13-2012 05:03 PM


All times are GMT -4. The time now is 12:42 AM.


MobileRead.com is a privately owned, operated and funded community.