Python Forum

Full Version: Percentages displayed as "0.00"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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)
Yes, indeed. Thanks for the tip.