![]() |
#1 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 8440
Join Date: Aug 2013
Device: Android tablet
|
Trying to make a percentage progress composit column.
Sorry if this isn't the right place to post this, but I'm trying to make a custom reading progress composite column which will calculate the percentage of a book read by taking manually entered information from the #pages_read and #total_pages custom columns. I know I'd need to divide #pages_read by #total_pages and then multiply by 100, but I'm really not very good with this coding stuff and can't seem to figure it out. If possible I'd also want the progress composite column to be blank at 0%.
I've tried search for this quite a bit already, so if anyone can point me in the right direction, I'd really appreciate it. Thanks. |
![]() |
![]() |
![]() |
#2 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Try this:
Code:
program: read = raw_field('#pages_read'); total = raw_field('#total_pages'); # if total <= 0 (or undefined), set read to 0 read = cmp(total, 0, 0, 0, read); # make sure the dividend (total) != 0 total = cmp(total, 0, 1, 1, total); percent = multiply(divide(read, total), 100); cmp(percent, 0, '', '', format_number(percent, "{0:2d}")) |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 8440
Join Date: Aug 2013
Device: Android tablet
|
Thanks very much! This worked perfectly. I just had to tinker with it a bit to figure out how to get the % symbol in there, but it looks exactly how I wanted it. You're a life saver!
|
![]() |
![]() |
![]() |
#4 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
![]() And you are welcome. |
|
![]() |
![]() |
![]() |
#5 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 8440
Join Date: Aug 2013
Device: Android tablet
|
|
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 8440
Join Date: Aug 2013
Device: Android tablet
|
So um I've run into a problem that I can't seem to figure out. It doesn't seem to want to display the percentage on more than one book entry at at time. Any other rows on the spreadsheet are showing up blank like they would if it was 0% regardless of what the actual percentage is. This problem seems to be there regardless of whether or not I have the percent symbol in the code.
|
![]() |
![]() |
![]() |
#7 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
|
|
![]() |
![]() |
![]() |
#8 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 8440
Join Date: Aug 2013
Device: Android tablet
|
@chaley After playing around with it a bit more it seems like even though the first time I tested this composite column template, it worked perfectly, now it seems like it just wants to return a blank for anything less than 100%. When it's at 100%, it will show it.
If you still need me to, I'll try to get that database mailed to you when I have some free time later. |
![]() |
![]() |
![]() |
#9 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
EDIT: also give me the custom column types for #pages_read and #total_pages. In particular, are they a numeric type (integer or floating point)? Last edited by chaley; 12-26-2019 at 02:04 PM. |
|
![]() |
![]() |
![]() |
#10 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 8440
Join Date: Aug 2013
Device: Android tablet
|
It's basically what you suggested with just the percent symbol added in but removing it didn't fix the problem.
Code:
program: read = raw_field('#pages_read'); total = raw_field('#total_pages'); # if total <= 0 (or undefined), set read to 0 read = cmp(total, 0, 0, 0, read); # make sure the dividend (total) != 0 total = cmp(total, 0, 1, 1, total); percent = multiply(divide(read, total), 100); cmp(percent, 0, '', '', format_number(percent, "{0:2d}%")) |
![]() |
![]() |
![]() |
#11 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
change the format in the last line to be "{0:2.0f}%". The problem seems to be that the "d" format chokes if the input is an actual floating point number.
|
![]() |
![]() |
![]() |
#12 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 8440
Join Date: Aug 2013
Device: Android tablet
|
I suppose I don't fully understand what floating point numbers are or how they apply here since I thought they were just any number with an exponent such as 200 to the power of 10 (not sure how to format the superscript here).
Regardless, your fix appears to have worked after entering test data into a few book entries. Thank you again for your help! |
![]() |
![]() |
![]() |
#13 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
The format code "d" stands for decimal, aka integer. The format code "f" stands for float. Apparently the code that does the conversion to character (prints) for integer is unable to handle a float. This is understandable because it would need to silently throw away any fraction. The code for "float" is able to handle an integer, understandable because an integer's fractional part is zero. That is why in your case 'f' generally works, while 'd' works only when the result of the divide is an integer (no fraction, e.g. 200/50). More than you wanted to know. ![]() |
|
![]() |
![]() |
![]() |
#14 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 8440
Join Date: Aug 2013
Device: Android tablet
|
Thanks for the explanation. I can see now why there are issues. When calculating a percentage you can get decimals even though the original numbers you were calculating were whole numbers. Though this now allows me to display a more precise percentage of a decimal or two if I like. I set it to show two decimal places, but I may change it later if I decide I don't like it.
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Showing only percentage in reading progress | mwj22 | Kindle Developer's Corner | 13 | 03-21-2017 09:18 AM |
Make job progress indicator more useful | THenrich | Recipes | 6 | 07-30-2014 02:53 AM |
Removing/Hiding Percentage or entire Progress Bar | lopan24 | Kindle Developer's Corner | 22 | 04-19-2014 01:11 PM |
Percentage progress issue | Cameronpaterson | Kobo Tablets | 3 | 03-27-2012 09:42 AM |
Progress Indicator for K2 Now Has a Percentage Also! | Lilly | Amazon Kindle | 46 | 02-14-2009 11:22 PM |