formatters.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/env python
  2. #Copyright ReportLab Europe Ltd. 2000-2017
  3. #see license.txt for license details
  4. #history https://hg.reportlab.com/hg-public/reportlab/log/tip/src/reportlab/lib/formatters.py
  5. __all__=('Formatter','DecimalFormatter')
  6. __version__='3.3.0'
  7. __doc__="""
  8. These help format numbers and dates in a user friendly way.
  9. Used by the graphics framework.
  10. """
  11. import string, sys, os, re
  12. class Formatter:
  13. "Base formatter - simply applies python format strings"
  14. def __init__(self, pattern):
  15. self.pattern = pattern
  16. def format(self, obj):
  17. return self.pattern % obj
  18. def __repr__(self):
  19. return "%s('%s')" % (self.__class__.__name__, self.pattern)
  20. def __call__(self, x):
  21. return self.format(x)
  22. _ld_re=re.compile(r'^\d*\.')
  23. _tz_re=re.compile('0+$')
  24. class DecimalFormatter(Formatter):
  25. """lets you specify how to build a decimal.
  26. A future NumberFormatter class will take Microsoft-style patterns
  27. instead - "$#,##0.00" is WAY easier than this."""
  28. def __init__(self, places=2, decimalSep='.', thousandSep=None, prefix=None, suffix=None):
  29. if places=='auto':
  30. self.calcPlaces = self._calcPlaces
  31. else:
  32. self.places = places
  33. self.dot = decimalSep
  34. self.comma = thousandSep
  35. self.prefix = prefix
  36. self.suffix = suffix
  37. def _calcPlaces(self,V):
  38. '''called with the full set of values to be formatted so we can calculate places'''
  39. self.places = max([len(_tz_re.sub('',_ld_re.sub('',str(v)))) for v in V])
  40. def format(self, num):
  41. # positivize the numbers
  42. sign=num<0
  43. if sign:
  44. num = -num
  45. places, sep = self.places, self.dot
  46. strip = places<=0
  47. if places and strip: places = -places
  48. strInt = ('%.' + str(places) + 'f') % num
  49. if places:
  50. strInt, strFrac = strInt.split('.')
  51. strFrac = sep + strFrac
  52. if strip:
  53. while strFrac and strFrac[-1] in ['0',sep]: strFrac = strFrac[:-1]
  54. else:
  55. strFrac = ''
  56. if self.comma is not None:
  57. strNew = ''
  58. while strInt:
  59. left, right = strInt[0:-3], strInt[-3:]
  60. if left == '':
  61. #strNew = self.comma + right + strNew
  62. strNew = right + strNew
  63. else:
  64. strNew = self.comma + right + strNew
  65. strInt = left
  66. strInt = strNew
  67. strBody = strInt + strFrac
  68. if sign: strBody = '-' + strBody
  69. if self.prefix:
  70. strBody = self.prefix + strBody
  71. if self.suffix:
  72. strBody = strBody + self.suffix
  73. return strBody
  74. def __repr__(self):
  75. return "%s(places=%d, decimalSep=%s, thousandSep=%s, prefix=%s, suffix=%s)" % (
  76. self.__class__.__name__,
  77. self.places,
  78. repr(self.dot),
  79. repr(self.comma),
  80. repr(self.prefix),
  81. repr(self.suffix)
  82. )
  83. if __name__=='__main__':
  84. def t(n, s, places=2, decimalSep='.', thousandSep=None, prefix=None, suffix=None):
  85. f=DecimalFormatter(places,decimalSep,thousandSep,prefix,suffix)
  86. r = f(n)
  87. print("places=%2d dot=%-4s comma=%-4s prefix=%-4s suffix=%-4s result=%10s %s" %(f.places, f.dot, f.comma, f.prefix, f.suffix,r, r==s and 'OK' or 'BAD'))
  88. t(1000.9,'1,000.9',1,thousandSep=',')
  89. t(1000.95,'1,001.0',1,thousandSep=',')
  90. t(1000.95,'1,001',-1,thousandSep=',')
  91. t(1000.9,'1,001',0,thousandSep=',')
  92. t(1000.9,'1000.9',1)
  93. t(1000.95,'1001.0',1)
  94. t(1000.95,'1001',-1)
  95. t(1000.9,'1001',0)
  96. t(1000.1,'1000.1',1)
  97. t(1000.55,'1000.6',1)
  98. t(1000.449,'1000.4',-1)
  99. t(1000.45,'1000',0)