Advertisement
rfmonk

decimal_create.py

Jan 22nd, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # credit for this goes to the author
  4. # of The Python Standard Library by example
  5. # Jesse Noller, Python Core Developer and PSF Board Member
  6. # Book can be purchased on amazon, it is also
  7. # on the oreilly bookshelf and may be at your
  8. # library. I'm only reprinting the code example
  9. # here because I'm working on this stuff as a
  10. # student of the Python Programming language.
  11. # Thanks Jesse Noller for such a good book.
  12.  
  13. import decimal
  14.  
  15. fmt = '{0:<25} {1:<25}'
  16. print fmt.format('Input', 'Output')
  17. print fmt.format('-' * 25, '-' * 25)
  18.  
  19. # Integer
  20. print fmt.format(5, decimal.Decimal(5))
  21.  
  22. # String
  23. print fmt.format('3.14', decimal.Decimal('3.14'))
  24.  
  25. # Float
  26. f = 0.1
  27. print fmt.format(repr(f), decimal.Decimal(str(f)))
  28. print fmt.format('%23g' % f,
  29.                  str(decimal.Decimal.from_float(f))[:25])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement