|
|
#1 |
|
Member
![]() Posts: 13
Karma: 10
Join Date: Sep 2011
Device: iphone 4
|
Custom Column with division
Alright, this may be an easy question to answer but since my program experience is if/then statements and some initial basic that was self=taught on tandy's a *long* time ago, I'm just knowledgeable enough to be dangerous as they say. However, for this issue, I can't get it quite how I'd like it. I've been screwing around with this for about an hour trying to get it how I'd like, and while I got it partially to work the way I want it, I can live with it if necessary, but I'd prefer to get it done as I want it.
My problem. I have two columns, "#chapters" and "#reviews" that I want to use to create a custom column. This new custom column is a simple division of review/chapters. I'd like to have a value (with one decimal) or no value at all (not 0). Initially I just used a simple divide function: divide(field('#reviews'), field('#chapters')) However, because this database has a lot of stories in it, some with only chapters, some with no chapters either, I was getting an error code. In my mind this is unacceptable, so I used the CMP function (don't know if that was the right way to go) to insure everything had a value. I'd did this because there's an example of how to do it with something else on the board, and I was able to understand how the function worked through the example. It's probably not the best way to do it, since I want a null value if "#reviews" is not available. I'm guessing I have to use "ifempty" somehow, but I'm not 100% sure how to write it out. Currently I have this: format_number( reviews_val=cmp(field("#reviews"),1,1, field("#reviews"), field("#reviews")); chapter_val=cmp(field("#chapters"),1,1, field("#chapters"), field("#chapters")); divide(reviews_val,chapter_val),"{0:5.1f}") So everything has a value, some of them are small (0.0) or (0.1) because there's no actual 'reviews' to divide by, even if there's chapters. What I'd like is a formula that basically leaves the column blank if there's no review value or no chapter value. I'm not 100% sure this is possible, but it likely is. So, anybody out there willing to help me out? Last edited by Metaxil; 12-14-2014 at 07:35 PM. |
|
|
|
|
|
#2 |
|
Ex-Helpdesk Junkie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 19,421
Karma: 85400180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
Try this. Sorry it's a little bulky, I have tried to comment the more obtuse bits.
Code:
program:
# if either is empty, return zero for reviews
r = test(
field('#chapters'),
ifempty(
field('#reviews'),
'0'
),
'0'
);
# don't divide by zero.
c = ifempty(
field('#chapters'),
'1'
);
ans = divide(r,c);
cmp(
ans,
0,
# if zero return blank
'',
'',
ans
)
Last edited by eschwartz; 12-15-2014 at 11:55 AM. Reason: fix typo with quoting field |
|
|
|
| Advert | |
|
|
|
|
#3 |
|
Member
![]() Posts: 13
Karma: 10
Join Date: Sep 2011
Device: iphone 4
|
That worked (with a modification and a fix). You forgot to put #review as '#review' (easily fixed) and I added a format_number at the beginning so I didn't have a 6 decimal answer.
The modified formula for anyone else interested Code:
program:format_number(
r = test(field('#chapters'),ifempty(field('#reviews'),'0'),'0');
c = ifempty(field('#chapters'),'1');ans = divide(r,c);
cmp(ans,0,'','',ans),"{0:5.1f}")
|
|
|
|
|
|
#4 |
|
Ex-Helpdesk Junkie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 19,421
Karma: 85400180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
hehe, sorry about that.
![]() Glad it worked though. format_number should be at the end though, enclosing cmp.
|
|
|
|
|
|
#5 | |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,529
Karma: 8075744
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
![]() As the language was designed and as eschwartz said, your template should be Code:
program:
r = test(field('#chapters'),ifempty(field('#reviews'),'0'),'0');
c = ifempty(field('#chapters'),'1');ans = divide(r,c);
format_number(cmp(ans,0,'','',ans),"{0:5.1f}")
|
|
|
|
|
| Advert | |
|
|
|
|
#6 |
|
Member
![]() Posts: 13
Karma: 10
Join Date: Sep 2011
Device: iphone 4
|
Alright. As I said in the first post. Dangerous. I have just enough knowledge to macgyver it most of the time, with trial and error testing methods. Plus I'm so used to working with nested statements, that's typically what I do. I actually was wondering if that was going to work the first time I tried it, but since it did, I didn't have to change anything, so that was good enough for me... and probably would have been until it broke. Thanks for fixing the code, I've changed it now.
I appreciate both your help with this. |
|
|
|
![]() |
| Thread Tools | Search this Thread |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Custom column returns value based on value of another custom column? | calvin-c | Calibre | 3 | 09-14-2013 03:24 PM |
| Custom yes/no column built from long text column | Philantrop | Library Management | 7 | 03-23-2013 08:44 PM |
| how to move value(s) of tag column to a custom made column | zoorakhan | Library Management | 0 | 12-08-2012 04:53 AM |
| custom date column from two state column | Dopedangel | Library Management | 7 | 01-03-2012 09:20 AM |
| Can custom book data be displayed in a custom column? | kiwidude | Development | 9 | 03-02-2011 06:35 AM |