1
|
1 |
|
import re |
2
|
1 |
|
from typing import Optional |
3
|
|
|
|
4
|
|
|
|
5
|
1 |
|
class MoneyCleaner: |
6
|
|
|
""" |
7
|
|
|
Utility class for converting numbers to numbers with decimal points. |
8
|
|
|
""" |
9
|
|
|
|
10
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
11
|
1 |
|
@staticmethod |
12
|
1 |
|
def clean(amount: Optional[str]) -> Optional[str]: |
13
|
|
|
""" |
14
|
|
|
Converts a number to a number with decimal point. |
15
|
|
|
|
16
|
|
|
:param amount: The input number. |
17
|
|
|
""" |
18
|
|
|
# Return empty input immediately. |
19
|
|
|
if not amount: |
20
|
|
|
return amount |
21
|
1 |
|
|
22
|
1 |
|
if re.search(r'^[0-9]{0,2}([. ][0-9]{3})+(,[0-9]{1,2})?$', amount): |
23
|
|
|
# Assume amount is in 1.123,12 or 1 123,12 or 1 123 format (Dutch). |
24
|
1 |
|
return amount.replace('.', '').replace(' ', '').replace(',', '.') |
25
|
|
|
|
26
|
1 |
|
if re.search(r'^[0-9]{0,2}([, ][0-9]{3})+(\.[0-9]{1,2})?$', amount): |
27
|
|
|
# Assume amount is in 1,123.12 or 1 123 format (Engels). |
28
|
1 |
|
return amount.replace(',', '').replace(' ', '') |
29
|
|
|
|
30
|
1 |
|
if re.search(r'^[0-9]+(,[0-9]{1,2}$)', amount): |
31
|
|
|
# Assume amount is in 123,12 or in 123,1 format (Dutch). |
32
|
1 |
|
return amount.replace(',', '.') |
33
|
|
|
|
34
|
1 |
|
if re.search(r'^[0-9]{0,2}(\.[0-9]{3})+(\.[0-9]{2})?$', amount): |
35
|
|
|
# Amount is in 1.123.12 format. |
36
|
|
|
return amount.replace('.', '', 1) |
37
|
1 |
|
|
38
|
|
|
# Format of amount is not recognized. Return amount. |
39
|
|
|
return amount |
40
|
|
|
|
41
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
42
|
|
|
|