Completed
Push — master ( 0c702e...f32b73 )
by P.R.
01:15
created

MoneyCleaner   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B clean() 0 33 5
1
import re
2
3
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
44
# ----------------------------------------------------------------------------------------------------------------------
45