Welcome, guest | Sign In | My Account | Store | Cart

-1.234e+018 --> '-1234000000000000000.0'

Python, 45 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def non_exp_repr(x):
    """Return a floating point representation without exponential notation.

    Result is a string that satisfies:
        float(result)==float(x) and 'e' not in result.
    
    >>> non_exp_repr(1.234e-025)
    '0.00000000000000000000000012339999999999999'
    >>> non_exp_repr(-1.234e+018)
    '-1234000000000000000.0'
    
    >>> for e in xrange(-50,51):
    ...     for m in (1.234, 0.018, -0.89, -75.59, 100/7.0, -909):
    ...         x = m * 10 ** e
    ...         s = non_exp_repr(x)
    ...         assert 'e' not in s
    ...         assert float(x) == float(s)

    """
    s = repr(float(x))
    e_loc = s.lower().find('e')
    if e_loc == -1:
        return s

    mantissa = s[:e_loc].replace('.', '')
    exp = int(s[e_loc+1:])

    assert s[1] == '.' or s[0] == '-' and s[2] == '.', "Unsupported format"     
    sign = ''
    if mantissa[0] == '-':
        sign = '-'
        mantissa = mantissa[1:]

    digitsafter = len(mantissa) - 1     # num digits after the decimal point
    if exp >= digitsafter:
        return sign + mantissa + '0' * (exp - digitsafter) + '.0'
    elif exp <= -1:
        return sign + '0.' + '0' * (-exp - 1) + mantissa
    ip = exp + 1                        # insertion point
    return sign + mantissa[:ip] + '.' + mantissa[ip:]


if __name__ == '__main__':
    import doctest
    print 'Doctest results:', doctest.testmod()

Sometimes you need to print or store floats without exponential notation.

The need arises in xmlrpc where the spec only allows decimal point notation. Also, for some applications, exponential notation is inappropriate for user output (i.e. new cars discounted to only 2.3499e+005).

Created by Raymond Hettinger on Mon, 13 Dec 2004 (PSF)
Python recipes (4591)
Raymond Hettinger's recipes (97)

Required Modules

Other Information and Tasks