Python Forum
Percentages displayed as "0.00" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Percentages displayed as "0.00" (/thread-22508.html)



Percentages displayed as "0.00" - Winfried - Nov-15-2019

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.


RE: Percentages displayed as "0.00" - ibreeden - Nov-15-2019

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)



RE: Percentages displayed as "0.00" - Winfried - Nov-15-2019

Yes, indeed. Thanks for the tip.