diff --git a/mathparse/mathparse.py b/mathparse/mathparse.py index 8aaab68..7b99b13 100644 --- a/mathparse/mathparse.py +++ b/mathparse/mathparse.py @@ -85,19 +85,23 @@ def to_number(val) -> Union[int, float, str, Decimal]: """ Convert a string to an int or float if possible. """ - # If already a number (int, float, Decimal), return as-is - if isinstance(val, (int, float, Decimal)): - return val + # If already a number (Decimal), return as-is + if isinstance(val, Decimal): + return Decimal(val) + + # Convert other numbers to Decimal + if isinstance(val, (int, float)): + return Decimal(val) # Check if it's a constant and convert if is_constant(val): - return mathwords.CONSTANTS[val] + return Decimal(mathwords.CONSTANTS[val]) # Otherwise, try to convert string to number if is_int(val): - return int(val) + return Decimal(int(val)) elif is_float(val): - return float(val) + return Decimal(float(val)) return val diff --git a/tests/test_complex_statements.py b/tests/test_complex_statements.py index d79b439..0cc2b3c 100644 --- a/tests/test_complex_statements.py +++ b/tests/test_complex_statements.py @@ -78,3 +78,10 @@ def test_unicode_pi_in_expression(self): """ result = mathparse.parse('π + 1') self.assertAlmostEqual(result, 3.141593 + 1, places=5) + + def test_with_operations_and_single_numbers(self): + """ + Test using the unicode π symbol in a complex expression. + """ + result = mathparse.parse("(1 + 3 + 2.0 + 6) * 1") + self.assertAlmostEqual(result, 0.00012, places=5)