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