ATLAS Offline Software
thousands.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 # Because the obvious solution doesn't work on lxplus :(
4 
5 # import locale
6 # locale.setlocale(locale.LC_NUMERIC, "")
7 # locale.format("%i", num, True)
8 
9 # http://code.activestate.com/recipes/498181-add-thousands-separator-commas-to-formatted-number/
10 
11 # Code from Michael Robellard's comment made 28 Feb 2010
12 def splitThousands(s, tSep=',', dSep='.'):
13  if s is None:
14  return 0
15  from builtins import int
16  if isinstance(s, int) or isinstance(s, float):
17  s = str(s)
18  if s[0] == '-' or s[0] == '+':
19  lhs=s[0]
20  s=s[1:]
21  else:
22  lhs=''
23  if dSep != '' and s.rfind(dSep)>0:
24  rhs=s[s.rfind(dSep)+1:]
25  s=s[:s.rfind(dSep)]
26  if len(s) <= 3: return lhs + s + dSep + rhs
27  return lhs + splitThousands(s[:-3], tSep) + tSep + s[-3:] + dSep + rhs
28  else:
29  if len(s) <= 3: return lhs + s
30  return lhs + splitThousands(s[:-3], tSep) + tSep + s[-3:]
python.ext.thousands.splitThousands
def splitThousands(s, tSep=',', dSep='.')
Definition: thousands.py:12
str
Definition: BTagTrackIpAccessor.cxx:11