View Single Post
Old 08-25-2019, 03:55 PM   #3
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,448
Karma: 8012886
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}");
The reason I used "raw_field" is that my column was formatted with currency signs and commas, and I wanted to retrieve only the base number.
chaley is offline   Reply With Quote