Column with Ratio of 2 other Columns?
#1  MarjaE 02-15-2020, 08:27 PM

I'd like to set up a column tracking the ratio of size to #pages. My ideal format would be #ratio = (size*100)/(#pages+1).

The +1 is to avoid division by zero bugs when #pages is unknown.

I've looked in the manual, but the only ratios mentioned are aspect ratios.

I've looked in this thread:

And wrote this into "Template," but just get TEMPLATE ERROR

program: divide(field("size"),("#pages"+1));

So, uh, how should users build columns as ratios?

#2  davidfor 02-15-2020, 09:21 PM
The problem is that the addition has to be done in a similar way to the division. So, it should be:

program: divide(field("size"),add(field("#pages"),1));
But, that actually gives zero for everything as the "size" column is always zero. You need to use the booksize function:

program: divide(booksize(),add(field("#pages"),1));
"booksize()" is the size in bytes for one of the formats for the book. It looks like it is the size of the largest format.

#3  MarjaE 02-15-2020, 09:54 PM
Thank you! Is there a way to round off or truncate the results?

#4  davidfor 02-15-2020, 10:23 PM
You need to use "format_number":

program: format_number(divide(booksize(),add(raw_field("#pages"),1)),"{0:,.0f}")
The template editor has some details about the format string.

And using "raw_field" is a bit safer. I had an error on books with more than 1000 pages as my pages columns is formatted with commas.

#5  MarjaE 02-21-2020, 06:00 PM
Thank you, that works nicely!

Today's Posts | Search this Thread | Login | Register