Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making decimal.Decimal the default numerical type in Python

Is there some way I could make decimal.Decimal the default type for all numerical values in Python? I would like to be able to use Python in a manner similar to the bc and dc programs without having to call decimal.Decimal(...) for every number.

EDIT: For the uninitiated: bc.

EDIT 2: Thank you tokenize module..

like image 467
Eric Pruitt Avatar asked Jan 18 '11 17:01

Eric Pruitt


People also ask

Are decimals Numeric Python?

By definition, isdecimal() ⊆ isdigit() ⊆ isnumeric() . That is, if a string is decimal , then it'll also be digit and numeric .

How do you declare a decimal value in Python?

If value is a tuple , it should have three components, a sign ( 0 for positive or 1 for negative), a tuple of digits, and an integer exponent. For example, Decimal((0, (1, 4, 1, 4), -3)) returns Decimal('1.414') .


1 Answers

At the bottom of the tokenize module's documentation, there is a function that does exactly what I need:

  • Python 3: "Example of a script rewriter that transforms float literals into Decimal objects"
  • Python 2: "Example of a script re-writer that transforms float literals into Decimal objects"
like image 98
Eric Pruitt Avatar answered Oct 02 '22 02:10

Eric Pruitt