![]() |
#1 |
Member
![]() Posts: 21
Karma: 10
Join Date: May 2011
Device: PRS-600
|
Help with test template formula
I have created the below template:
program:format_number(test(field('#duration'),divi de(field('#page_count'),field('#duration')),0),"{0 :5.1f}"); What I was intending it would do is return 0 where the 'duration' field is blank (thereby avoiding division by zero) else the quotient of page count divided by duration value. What is actually happening is the result is never 0, it is either the quotient or a division by zero error, so it appears the test is ignored or I am misunderstanding the purpose of the test function. Screenshot attached for context. Any ideas? Last edited by China Diapers; 08-25-2019 at 03:23 PM. |
![]() |
![]() |
![]() |
#2 | |
Member
![]() Posts: 21
Karma: 10
Join Date: May 2011
Device: PRS-600
|
Quote:
program:test(field('#duration'),"True","False"); |
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,412
Karma: 8012664
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Your problem comes from the fact that arguments to template functions are all evaluated before the template function itself. This means that the divide is calculated even when the test is false. Said another way, the test is an "&" and not an "&&". There is no shortcutting.
You can fix it by computing a legal divisor that is used in the test, but with the test returning the "else" value. This template is an example. I didn't use your column names, but it should be clear enough. program: Code:
program: dividend = raw_field('#myint2'); divisor = ifempty(field('#myint'), 1); format_number( test(field('#myint'), divide(dividend, divisor), 0), "{0:5.1f}"); |
![]() |
![]() |
![]() |
#4 | |
Member
![]() Posts: 21
Karma: 10
Join Date: May 2011
Device: PRS-600
|
Quote:
|
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Using built-in template functions in a custom template function | ilovejedd | Library Management | 4 | 01-28-2018 12:20 PM |
There *IS* a formula! | cromag | Lounge | 5 | 01-10-2015 10:21 PM |
Troubleshooting Kindle and math formula | DrShakalu | Amazon Kindle | 12 | 12-11-2011 07:25 AM |
Formula Plots | PuxyYunm | General Discussions | 9 | 05-15-2011 04:19 AM |