Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 12-21-2019, 09:55 AM   #1
Kythe42
Zealot
Kythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipse
 
Kythe42's Avatar
 
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.
Kythe42 is offline   Reply With Quote
Old 12-21-2019, 12:07 PM   #2
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,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}"))
The custom column definition looks like this:
Attached Thumbnails
Click image for larger version

Name:	Clipboard01.jpg
Views:	196
Size:	41.0 KB
ID:	175795  
chaley is offline   Reply With Quote
Advert
Old 12-21-2019, 12:48 PM   #3
Kythe42
Zealot
Kythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipse
 
Kythe42's Avatar
 
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!
Kythe42 is offline   Reply With Quote
Old 12-21-2019, 12:50 PM   #4
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Kythe42 View Post
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!
I left the % out because with it the column doesn't sort correctly. That might not be an issue for you.

And you are welcome.
chaley is offline   Reply With Quote
Old 12-21-2019, 01:20 PM   #5
Kythe42
Zealot
Kythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipse
 
Kythe42's Avatar
 
Posts: 122
Karma: 8440
Join Date: Aug 2013
Device: Android tablet
Quote:
Originally Posted by chaley View Post
I left the % out because with it the column doesn't sort correctly. That might not be an issue for you.

And you are welcome.
Ah I didn't consider that. If it turns out to be an issue for me, then I'll take it out. Thanks again!
Kythe42 is offline   Reply With Quote
Advert
Old 12-26-2019, 02:00 AM   #6
Kythe42
Zealot
Kythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipse
 
Kythe42's Avatar
 
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.
Kythe42 is offline   Reply With Quote
Old 12-26-2019, 04:36 AM   #7
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Kythe42 View Post
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.
The easiest way for me to help is to have a copy of your book database, metadata.db. Send it to thaw1 at charleshaley dot org (make the substitutions). If the db file is larger than 2 megabytes then please zip it first. Don't send any books.
chaley is offline   Reply With Quote
Old 12-26-2019, 01:49 PM   #8
Kythe42
Zealot
Kythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipse
 
Kythe42's Avatar
 
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.
Kythe42 is offline   Reply With Quote
Old 12-26-2019, 02:00 PM   #9
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Kythe42 View Post
@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.
Post the template you are using. If I can see the problem from that then no need for the db. If not ...

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.
chaley is offline   Reply With Quote
Old 12-26-2019, 02:08 PM   #10
Kythe42
Zealot
Kythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipse
 
Kythe42's Avatar
 
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}%"))
The #pages_read and #total_pages custom columns are integers.
Kythe42 is offline   Reply With Quote
Old 12-26-2019, 02:14 PM   #11
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,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.
chaley is offline   Reply With Quote
Old 12-26-2019, 02:41 PM   #12
Kythe42
Zealot
Kythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipse
 
Kythe42's Avatar
 
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!
Kythe42 is offline   Reply With Quote
Old 12-26-2019, 04:50 PM   #13
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Kythe42 View Post
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).
Floating point numbers (often called "real" numbers) are distinguished from integers by having a fractional part. For example, "1" is an integer while "1.5" and "1 2/3" are floats. Exponentials are not directly related to this difference. Whether or not the result of the exponent operation is an integer or a float depends on whether the result has a fractional part.

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.
chaley is offline   Reply With Quote
Old 12-26-2019, 05:14 PM   #14
Kythe42
Zealot
Kythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipseKythe42 can illuminate an eclipse
 
Kythe42's Avatar
 
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.
Kythe42 is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
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


All times are GMT -4. The time now is 11:46 PM.


MobileRead.com is a privately owned, operated and funded community.