pythondecimalstandard-libraryfractions# Best way to convert fractions.Fraction to decimal.Decimal?

In Python, the `fractions.Fraction`

and `decimal.Decimal`

standard library classes exist to help keep arithmetic with rational numbers precise. For the unfamiliar, an example of where it helps:

```
>>> 1 / 10 * 3
0.30000000000000004
>>> decimal.Decimal('1') / 10 * 3
Decimal('0.3')
>>> fractions.Fraction('1') / 10 * 3
Fraction(3, 10)
```

My question is, if I have a `Fraction`

, what's the best way to convert it to a `Decimal`

?

Unfortunately the obvious solution doesn't work:

```
>>> decimal.Decimal(fractions.Fraction(3, 10))
Traceback (most recent call last):
...
TypeError: conversion from Fraction to Decimal is not supported
```

Right now I'm using this code:

```
>>> decimal.Decimal(float(fractions.Fraction(3, 10)))
Decimal('0.299999999999999988897769753748434595763683319091796875')
```

Now, when I actually output this value, any amount of rounding will convert it to 0.3, and I only do this conversion immediately before output (all the core math is done with `Fraction`

). Still, it seems a bit silly to me that I can't get a `Decimal('0.3')`

from a `Fraction(3, 10)`

. Any help would be appreciated!

Solution

How about leaving the division of the fraction to `Decimal()`

itself?

```
def decimal_from_fraction(frac):
return frac.numerator / decimal.Decimal(frac.denominator)
```

This is what `Fraction.__float__()`

does (simply divide the numerator by the denominator), but by turning at least one of the two values into a `Decimal`

object you get to control the output.

This lets you use the `decimal`

context:

```
>>> decimal_from_fraction(fractions.Fraction(3, 10))
Decimal('0.3')
>>> decimal_from_fraction(fractions.Fraction(1, 55))
Decimal('0.01818181818181818181818181818')
>>> with decimal.localcontext() as ctx:
... ctx.prec = 4
... decimal_from_fraction(fractions.Fraction(1, 55))
...
Decimal('0.01818')
```

- Python Jinja2 LaTeX Table
- Getting attributes of a class
- How can I print many significant figures in Python?
- How to allow list append() method to return the new list
- Calculate Last Friday of Month in Pandas
- Python type hint for Iterable[str] that isn't str
- How to iterate over a list in chunks
- How to exit the entire application from a Python thread?
- Running shell command and capturing the output
- How do I pass a variable by reference?
- Convert range(r) to list of strings of length 2 in python
- How can I get the start and end dates for each week?
- how to use send_message() in python-telegram-bot
- Python conditional replacement based on element type
- How can I count the number of items in an arbitrary iterable (such as a generator)?
- Find longest consecutive range of numbers in list
- Insert text in braces with asyncpg
- How does one put a link / url to the web-site's home page in Django?
- How to determine if a path is a subdirectory of another?
- Custom Keybindings for Ipython terminal
- FastAPI asynchronous background tasks blocks other requests?
- How to make sure that information from one file is duplicated into several text documents, without specific lines
- Installing a Python environment with Anaconda
- sklearn pipeline model predicting same results for all input
- Brew command not found after installing Anaconda Python
- How to get an XPath from selenium webelement or from lxml?
- Pipe PuTTY console to Python script
- How to align the axes of a figure in matplotlib?
- Persist ParentDocumentRetriever of langchain
- How to reset index in a pandas dataframe?