6

I have a Dollar price as a Decimal with a precision of .01 (to the cent.)

I want to display it in string formatting, like having a message "You have just bought an item that cost $54.12."

The thing is, if the price happens to be round, I want to just show it without the cents, like $54.

How can I accomplish this in Python? Note that I'm using Python 2.7, so I'd be happy to use new-style rather than old-style string formatting.

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
  • Look at this answer from [Removing Trailing Zeros in Python](http://stackoverflow.com/a/5808014/63011) – Paolo Moretti Mar 04 '12 at 18:29
  • @PaoloMoretti: I wouldn't want an algorithm. I'd want to use Python's built-in faculties. If impossible, I'll make my own algorithm. – Ram Rachum Mar 04 '12 at 18:39

5 Answers5

6
>>> import decimal
>>> n = decimal.Decimal('54.12') 
>>> print('%g' % n)
'54.12'
>>> n = decimal.Decimal('54.00') 
>>> print('%g' % n)
'54'
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
ezod
  • 7,261
  • 2
  • 24
  • 34
  • @DavidHall: Hey, you're right. Sorry. I'll retract my previous comment. Interestingly, the new formatting language doesn't work this way: `"{0:g}".format(decimal.Decimal("54.00"))` returns `54.00`! – Tim Pietzcker Mar 04 '12 at 18:43
  • 1
    This will round the number in some cases. For example `print('%g' % decimal.Decimal(54.99999))` outputs `55`. – Paolo Moretti Mar 04 '12 at 18:57
  • Problem with this solution: `'%g' % 1000000 == '1e+06'`. Scientific notation is not a proper way to display dollar prices. – Ram Rachum Mar 04 '12 at 18:57
  • @RamRachum: Well then use jcollado's solution. It's the only way. – Tim Pietzcker Mar 05 '12 at 07:15
2

Answer is taken from Python Decimals format

>>> a=54.12
>>> x="${:.4g}".format(a)
>>> print x
   $54.12
>>> a=54.00
>>> x="${:.4g}".format(a)
>>> print x
   $54
Community
  • 1
  • 1
redratear
  • 251
  • 2
  • 3
1

I'd do something like this:

import decimal

a = decimal.Decimal('54.12')
b = decimal.Decimal('54.00')

for n in (a, b):
    print("You have just bought an item that cost ${0:.{1}f}."
          .format(n, 0 if n == n.to_integral() else 2))

where {0:.{1}f} means print the first argument as a float using the number of decimals in the second argument and the second argument is 0 when the number is actually equal to its integer version and 2 when is not which I believe is what you're looking for.

The output is:

You have just bought an item that cost $54.12.

You have just bought an item that cost $54

Community
  • 1
  • 1
jcollado
  • 39,419
  • 8
  • 102
  • 133
  • 4
    I don't think that's what he wants. He wants `$54.12` as output. Only if the value is `54.00` he wants the decimals chopped off. – Tim Pietzcker Mar 04 '12 at 18:22
  • What advantage adds the `decimal` module? It seems useless. – Zenon Mar 04 '12 at 18:23
  • 2
    @Zenon: from [the documentation](http://docs.python.org/library/decimal.html): "Unlike hardware-based binary floating point, the `decimal` module has a user-alterable precision (defaulting to 28 places) which can be as large as needed for a given problem." – mechanical_meat Mar 04 '12 at 18:28
  • @TimPietzcker Thanks for your comment. I certainly misunderstood the question. I've just updated the answer to cover both cases. – jcollado Mar 04 '12 at 18:41
0

Is this what you want?

Note: x is the original price.

round = x + 0.5
s = str(round)
dot = s.find('.')
print(s[ : dot])
Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
Autumn K.
  • 33
  • 1
  • 1
  • 7
  • That's an algorithm. I wouldn't want an algorithm. I'd want to use Python's built-in faculties. If impossible, I'll use an algorithm. – Ram Rachum Mar 04 '12 at 18:42
-1
>>> dollars = Decimal(repr(54.12))
>>> print "You have just bought an item that cost ${}.".format(dollars)
You have just bought an item that cost $54.12.
Fred
  • 1,011
  • 1
  • 10
  • 36