This repository was archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
71 lines (61 loc) · 2.15 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
AppEngine has no support for locale
string.format() doesn not support formatting number with dots as thousand separators
But we need to format numbers.
This is our aproach.
@author Markus Tacker <[email protected]>
@copyright 2014 TLD dotHIV Registry GmbH | https://tld.hiv/
"""
class Format(object):
LOCALE_DE = 'de'
LOCALE_ES = 'es'
LOCALE_FR = 'fr'
LOCALE_PL = 'pl'
LOCALE_EN = 'en'
DEFAULT_LOCALE = 'en'
def __init__(self, locale=None):
self.locale = locale if locale else Format.DEFAULT_LOCALE
def decimal(self, number):
"""
Formats a decimal number.
"""
number = int(number)
if self.isDe():
return "{:,}".format(number).replace(',', '.')
return "{:,}".format(number)
def money(self, number, ratio=1.0):
"""
Formats a money value.
"""
number = float(number) * ratio
if self.isDe():
if number < 0.01: # Cent
pre, post = "{0:.1f} ct".format(number * 100).split('.')
elif number < 1.0: # Decimal Cent
return "{0:n} ct".format(number * 100)
else:
pre, post = "{0:,.2f} €".format(number).split('.')
pre = pre.replace(',', '.')
return ','.join((pre, post))
if number < 0.01: # Cent
return "€{0:.1f}¢".format(number * 100)
elif number < 1.0: # Decimal Cent
return "€{0:n}¢".format(number * 100)
return "€{0:,.2f}".format(number)
def decimalMoney(self, number, ratio=1.0):
"""
Formats a money value as decimal.
"""
decimal = self.decimal(float(number) * ratio)
if self.isDe():
return "%s €" % decimal
return "€%s" % decimal
def float(self, number):
"""
Formats a float number.
"""
if self.isDe():
return ','.join(("%.1f" % float(number)).split('.'))
return "%.1f" % float(number)
def isDe(self):
return self.locale in [Format.LOCALE_DE, Format.LOCALE_ES, Format.LOCALE_FR, Format.LOCALE_PL]