|
|
#1 |
|
Zealot
![]() Posts: 115
Karma: 22
Join Date: Sep 2011
Device: Acer Iconia 500w Tablet, PRS-T1
|
need help with nested template
I wanted to have a column that would tell me the average words per chapter of each book, so I created a custom column that is built from other columns. I already had a custom column for words and one for chapters. First I tried this code and received this exception: Code:
program:divide('#words', '#chapters')
EXCEPTION: could not convert string to float: #words
Here is my next attempt: Code:
program:
format_number('#words','{0:d}');
format_number('#chapters','{0:d}')
I then tried to combine the divide and format number function together: Code:
program:divide(format_number('#words','{0:d}'),format_number('#chapters','{0:d}'))
EXCEPTION: float division by zero
If I use the second and first code, I would need to assign the result from the format number functions to a variable in order to use the divide function, but I am unsure how to do so. Any suggestions on how I should change my code? |
|
|
|
|
|
#2 |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 3,853
Karma: 285886
Join Date: Jan 2010
Location: France
Device: Sony PRS-300, HTC WM6.5, Samsung Galaxy Nexus, HTC Sensation
|
Your problem comes from confusing constants with field (column) values. The string '#chapters' is a constant. It always has the value '#chapters', which as you can see is not a number. To get the value of a metadata field you use the 'field' function. This function takes the name of the field as its first argument. This argument is normally a constant, but certainly does not need to be.
Using the above, your first program would become: Code:
program:divide(field('#words'), field('#chapters'))
Code:
program: format_number(divide(field('#words'), field('#chapters')), '{0:5.0f}')
Code:
program:
# Get the value of the chapters field so we can use it more easily
chapters = raw_field('#chapters');
# Check if chapters is zero. If it is then put the empty string into the variable notIsZero
notIsZero = cmp(chapters, 0, 'yes', '', 'yes');
# Ensure that the chapters variable contains a non-zero number. We need this because the divide will happen
# even though we will throw the value away
chapters = test(notIsZero, chapters, '1');
# Return the result of the divide if notIsZero contains the non-empty string, otherwise return the empty string
test(notIsZero, format_number(divide(field('#words'), chapters), '{0:5.0f}'), '')
__________________
Support calibre developers at no cost to you by using calibre's get books feature. Use Calibre Companion to wirelessly connect your Android phone or tablet to calibre charles.haleys.org/calibre |
|
|
|
|
Enthusiast
|
|
|
|
#3 | ||
|
Zealot
![]() Posts: 115
Karma: 22
Join Date: Sep 2011
Device: Acer Iconia 500w Tablet, PRS-T1
|
Quote:
Quote:
Thanks. |
||
|
|
|
![]() |
| Thread Tools | Search this Thread |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Nested headings? | crich70 | Sigil | 20 | 04-11-2011 10:44 AM |
| Content Nested collections | fartang | Amazon Kindle | 1 | 04-09-2011 12:18 PM |
| Content nested collections | alexxx | Amazon Kindle | 6 | 06-22-2010 06:05 PM |
| Nested navPoint problem | Lemming | Calibre | 3 | 12-20-2009 11:24 AM |
| Are Nested TOCs Possible? | VikingDave | Sony Reader | 11 | 12-28-2007 07:58 PM |