GOAL:
I'm trying to display (assumed float) value as a decimal, within a print call using the '%d' operator.
PROBLEM
Documentation states that '%d' can only take the value of a decimal. Taking this into account, I imported the 'decimal' module and attempted to convert using the decimal function. The result was not altered, and my code would return what looks like a 'floored' price for my oranges (my oranges are not happy with that). What am I doing wrong?
CODE
import decimal
prices = {
"banana":4,
"apple":2,
"orange":1.5,
"pear":3
}
stock = {
"banana":6,
"apple":0,
"orange":32,
"pear":15
}
for x in prices:
print x
print "price: %d" % (decimal.Decimal(prices[x]))
for y in stock:
if y == x:
print "stock: %d" % (stock[y])
RESULT
orange
price: 1 // Need this to return the price (1.5)
stock: 32
pear
price: 3
stock: 15
banana
price: 4
stock: 6
apple
price: 2
stock: 0