2

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

2 Answers 2

1

If you want to print floating point numbers, use the %f specifier. For example, this will print the price with 2 decimal digits of precision:

print "price: %.2f" % prices[x]

See here for more documentation: http://docs.python.org/2/library/stdtypes.html#string-formatting

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer @Krzysztof Kosiński; should have made a point to check how many specifier types Python provides before trying to hack things. But regarding my code; I converted my value to a decimal, why did it not display a decimal version of my value?
If you look at the documentation link that Krzysztof Kosiński posted you can see that the 'd' format is a signed integer decimal format.
Based on the doc, if anything with a decimal point is considered a float, then how would you represent a 'decimal' (signed integer decimal)?' I ask because the 'd' specifier expects that, but apparently my price for oranges was a float.
@brooklynsweb: ah, I think I see what's confusing you. The word "decimal" here doesn't mean "decimal" as opposed to "integer", it means "decimal" as opposed to "hexadecimal", i.e. base 10. %d specified a signed integer in base 10.
The docs here use the word 'decimal' in its definition of "a number in a base 10 representation", not as "a number that's got a decimal point in its representation" or "an instance of the Python decimal.Decimal class". Compare to e.g. the %x format specifier that displays as hexadecimal.
0

Well, here's my attempt.

The %d specifier converts the number to an integer of base 10. So when you do something like:

print "price: %d"%Decimal("1.5")

It would print price:1.

To achieve what you want, you can use the %f specifier:

print "price: %.2f"%Decimal("1.5")

See more on formatting here. Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.