1

I'm trying to port some py2 code to py3, and I'd like to somehow reproduce the exact string representation of a floating number that py2 would have returned, but in python 3.

e.g.

from __future__ import print_function

a = 1.5 * 0.4
string = str(a)
print(string)

In python3, the output is 0.6000000000000001 , vs python2, where the output is 0.6, and I understand why:

Why does str(float) return more digits in Python 3 than Python 2?

However, is there a way where I can faithfully reproduce (in python3) what str(a) would have been (e.g. same behavior in py2 and py3), for arbitrary values of a?

I tried this

import six
a = 1.5 * 0.4
string = str(a)
if six.PY3:
    string = '{:.12g}'.format(a)
print(string)

The above "almost" works, but in the case a = 5 , we'll get different output (5.0 for py2 and 5 for py3)

asf107
  • 1,118
  • 7
  • 22

2 Answers2

1

The problem comes from the fact that as the same way some fractions are note easily represented in decimal form (for example 1/3 = 0.33333333…), some fractions are not easily represented in binary form (for example 1/3 = 0.01010101…). But if you want to have as consistent results as a decimal numbers, consider looking at the decimal module (which is available in Python 2.7 and 3). You could have something like this:

from decimal import Decimal

a = Decimal('1.5') * Decimal('0.4')
result = str(a)
print(result)
EvensF
  • 1,479
  • 1
  • 10
  • 17
0
>>> a = 1.5 * 0.4
>>> print(f'{a:.1f}')
0.6
nicholishen
  • 2,602
  • 2
  • 9
  • 13
  • 1
    `a = 1.55; print(f'{a:.1f}')` gives `1.6`. This is probably not desired. – Michael Butscher May 13 '20 at 01:40
  • yep, i certainly know how to format floats to arbitrary precision in either py2 or py3 , but what i dont know how to do is reproduce whatever py2's `str(a)` is doing. – asf107 May 13 '20 at 01:51