I want to get 1/7
with better precision, but it got truncated. How can I get better precision when I convert a rational number?
>>> str(1.0/7)[:50]
'0.142857142857'
Python has a built-in library for arbitrary-precision calculations: Decimal. For example:
>>>from decimal import Decimal, getcontext
>>>getcontext().prec = 50
>>>x = Decimal(1)/Decimal(7)
>>>x
Decimal('0.14285714285714285714285714285714285714285714285714')
>>>str(x)
'0.14285714285714285714285714285714285714285714285714'
Look at the Python Decimal documentation for more details. You can change the precision to be as high as you need.