Problem requires to output decimal proportion of negative integers in an unknown list. The desired answer is 0.500000 (6 decimal places), but my program only outputs 0.5. How do I keep the 5 terminal zeroes?
I've tried using the round(number, ndigits)
function and getcontext().prec
from the Decimal
library. All of these have yielded just 0.5.
def plusMinus(arr):
from decimal import getcontext, Decimal
getcontext().prec = 6
negamt = 0
posamt = 0
zeroamt = 0
for i in arr:
if i < 0:
negamt += 1
elif i > 0:
posamt += 1
else:
zeroamt += 1
print(Decimal(posamt)/Decimal(n), Decimal(negamt)/Decimal(n), Decimal(zeroamt)/Decimal(n))
The expected results for the unknown array are 0.500000, 0.333333, 0.166667; my program returns 0.5, 0.333333, 0.166667.