Ver código fonte

Remove "integral float" as a special case

Instead, the "{:g}" string formatter does the work of formatting
floats with integral values as ints when printing the result.
Ryan C. Thompson 6 anos atrás
pai
commit
c812764b7b
1 arquivos alterados com 2 adições e 6 exclusões
  1. 2 6
      roll.py

+ 2 - 6
roll.py

@@ -650,10 +650,6 @@ def _eval_expr_internal(
             for (op, nextval) in zip(operators, values[1:]):
                 opfun = op_dict[op]
                 result = opfun(result, nextval)
-            # https://github.com/python/mypy/issues/6060
-            if isinstance(result, float) and result.is_integer(): # type: ignore
-                # Corece integral floats to ints
-                result = int(result)
             return result
         else:
             # roll specification
@@ -786,7 +782,7 @@ if __name__ == '__main__':
             result = eval_expr(expr)
             print('Result: {result} (rolled {expr})'.format(
                 expr=color(expr_as_str(expr), EXPR_COLOR),
-                result=color(result, RESULT_COLOR),
+                result=color("{:g}".format(result), RESULT_COLOR),
             ))
         except Exception as exc:
             logger.error("Error while rolling: %s", repr(exc))
@@ -831,7 +827,7 @@ if __name__ == '__main__':
                         result = eval_expr(parsed['expr'], env)
                         print('Result: {result} (rolled {expr})'.format(
                             expr=color(expr_as_str(parsed, env), EXPR_COLOR),
-                            result=color(result, RESULT_COLOR),
+                            result=color("{:g}".format(result), RESULT_COLOR),
                         ))
                 print('')
             except KeyboardInterrupt: