1- from re import match , split
1+ from re import match , split , search , sub
22from numexpr import evaluate
33from views .layouts .display_layout import DisplayLayout
44
@@ -29,10 +29,30 @@ def clear(self):
2929 self .equationDisplay .setText ("" )
3030 self .resultDisplay .setText ("" )
3131
32- def calculate (self ):
32+ def calculate (self , ):
3333 if not bool (match (r"\+|-|x|/|%|\." , self .resultDisplay .text ()[- 1 ])):
34- equation = self .resultDisplay .text ().replace ("x" , "*" )
35- result = int (evaluate (equation ).item ()) if float (evaluate (equation ).item ()).is_integer () else float (evaluate (equation ).item ())
34+ self .equation = self .resultDisplay .text ().replace ("x" , "*" )
35+ if "%" in self .equation :
36+ self ._percentage ()
37+ item = evaluate (self .equation ).item ()
38+ result = int (item ) if float (item ).is_integer () else float (item )
3639
3740 self .equationDisplay .setText (self .resultDisplay .text ())
38- self .resultDisplay .setText (str (result ))
41+ self .resultDisplay .setText (str (result ))
42+
43+ def _percentage (self ):
44+ equation = search (r"([0-9]+)%([0-9]+)" , self .equation ).group (0 ).split ("%" )
45+ a , b = equation [0 ], equation [1 ]
46+ item = evaluate ("a / 100 * b" ,
47+ {
48+ "a" : int (a ) if float (a ).is_integer () else float (a ),
49+ "b" : int (b ) if float (b ).is_integer () else float (b )
50+ })
51+
52+ result = int (item ) if float (item ).is_integer () else float (item )
53+ self .equation = sub (r"([0-9]+)%([0-9]+)" , str (result ), self .equation )
54+
55+ print (self .equation )
56+
57+ if "%" in self .equation :
58+ self ._percentage ()
0 commit comments