2011-12-01 10 views
8

だから、これは正常に動作します:フォースパイソン標準形/科学表記/指数関数の形でフロート

>>> float(1.0e-1) 
0.10000000000000001 

しかし、より多くを扱うとき、それは印刷されません:

>>> float(1.0e-9) 
1.0000000000000001e-09 

これを強制する方法はありますか?たぶんnumpyや何かを使用します。

+1

[重複していない通常の形式で浮動小数点数を印刷する]の複製が可能です(http://stackoverflow.com/questions/7801586/print-a-float-number-in-normal-form-not-exponential) –

答えて

15
print '{0:.10f}'.format(1.0e-9) 

String formattingです。

+0

Doesn ' Python 2.6よりも前に動作しています(これは大きな問題ではありませんが、それを述べておくべきです)。もっと真剣に、これは小数点以下の桁数を修正します。これはおそらくOPが望んでいないものです。 – zwol

3

これは、特にフロートのための書式設定はかなり標準印刷、次のとおりです。f文字列フォーマットコードを使用することを示唆

print "%.9f" % 1.0e-9 
1
>>> a 
1.0000000000000001e-09 
>>> print "heres is a small number %1.9f" %a 
heres is a small number 0.000000001 
>>> print "heres is a small number %1.13f" %a 
heres is a small number 0.0000000010000 
>>> b 
11232310000000.0 
>>> print "heres is a big number %1.9f" %b 
heres is a big number 11232310000000.000000000 
>>> print "heres is a big number %1.1f" %b 
heres is a big number 11232310000000.0 
9

誰もが暗黙のうちに、それが後の桁数を修正しても大丈夫だと仮定しています小数点。それは私にとっては非常に不安な仮定のようです。しかし、あなたがその前提をしなければ、あなたが望むことをするための組み込みのメカニズムはありません。これは、同様の問題に直面したときに私が思いつく最高のハックです(PDFジェネレータでは、PDFの数字は指数表記を使用できません)。あなたはおそらく、文字列からすべてbを取りたいと思っています。ここには他のPython3-ismsがあるかもしれません。

_ftod_r = re.compile(
    br'^(-?)([0-9]*)(?:\.([0-9]*))?(?:[eE]([+-][0-9]+))?$') 
def ftod(f): 
    """Print a floating-point number in the format expected by PDF: 
    as short as possible, no exponential notation.""" 
    s = bytes(str(f), 'ascii') 
    m = _ftod_r.match(s) 
    if not m: 
     raise RuntimeError("unexpected floating point number format: {!a}" 
          .format(s)) 
    sign = m.group(1) 
    intpart = m.group(2) 
    fractpart = m.group(3) 
    exponent = m.group(4) 
    if ((intpart is None or intpart == b'') and 
     (fractpart is None or fractpart == b'')): 
     raise RuntimeError("unexpected floating point number format: {!a}" 
          .format(s)) 

    # strip leading and trailing zeros 
    if intpart is None: intpart = b'' 
    else: intpart = intpart.lstrip(b'0') 
    if fractpart is None: fractpart = b'' 
    else: fractpart = fractpart.rstrip(b'0') 

    if intpart == b'' and fractpart == b'': 
     # zero or negative zero; negative zero is not useful in PDF 
     # we can ignore the exponent in this case 
     return b'0' 

    # convert exponent to a decimal point shift 
    elif exponent is not None: 
     exponent = int(exponent) 
     exponent += len(intpart) 
     digits = intpart + fractpart 
     if exponent <= 0: 
      return sign + b'.' + b'0'*(-exponent) + digits 
     elif exponent >= len(digits): 
      return sign + digits + b'0'*(exponent - len(digits)) 
     else: 
      return sign + digits[:exponent] + b'.' + digits[exponent:] 

    # no exponent, just reassemble the number 
    elif fractpart == b'': 
     return sign + intpart # no need for trailing dot 
    else: 
     return sign + intpart + b'.' + fractpart 
0

使用%があなたの番号の印刷時E:

>>> a = 0.1234567 
>>> print 'My number is %.7e'%a 
My number 1.2345670e-01 

を使用すると、%gを使用している場合、それは自動的にあなたのための最良の可視化を選択します:

>>> print 'My number is %.7g'%a 
My number is 0.1234567 
1

ここzwolの答え簡素化と変換さです標準的なPython形式:

import re 
def format_float_in_standard_form(f): 
    s = str(f) 
    m = re.fullmatch(r'(-?)(\d)(?:\.(\d+))?e([+-]\d+)', s) 
    if not m: 
     return s 
    sign, intpart, fractpart, exponent = m.groups('') 
    exponent = int(exponent) + 1 
    digits = intpart + fractpart 
    if exponent < 0: 
     return sign + '0.' + '0'*(-exponent) + digits 
    exponent -= len(digits) 
    return sign + digits + '0'*exponent + '.0' 
関連する問題