Python Forum
Percentages displayed as "0.00"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Percentages displayed as "0.00"
#1
Hello,

This is a simple query where I need to display a quantity, and the percentage it represents.

I don't know if it's a Python or SQLite issue, but for some reason, the percentage column only displays "0.00" for all the rows:

cur.execute("SELECT NUMBER,1.0 * NUMBER / (SELECT SUM(NUMBER) FROM MyTable) AS PERCENTAGE FROM MyTable")
rows = cur.fetchall()
for row in rows:
	output = "{}\t{:.2f}".format(row['NUMBER'],row['PERCENTAGE'])
	print(output)
Can someone spot the error?

Thank you.
Reply
#2
If I had this I would first try to see directly from the database what the result is.
In this case it would be sufficient to remove the formatting. Just show what comes from the database.
And then: to get a percentage you need to multiply the ratio by 100. So instead of:
1.0 * NUMBER / (SELECT SUM(NUMBER) FROM MyTable)
... you should do:
100.0 * NUMBER / (SELECT SUM(NUMBER) FROM MyTable)
Reply
#3
Yes, indeed. Thanks for the tip.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,326 Oct-06-2023, 04:32 PM
Last Post: cubangt
  Table displayed out of order nigelwright7557 3 1,750 Nov-24-2021, 10:07 AM
Last Post: nigelwright7557
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,012 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  Auto-copy all displayed "print" results into clipboard? smallabc 1 1,620 Dec-16-2019, 02:10 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020