我如何格式化一个浮点数,使它不包含尾随零?换句话说,我希望得到的字符串尽可能短。

例如:

3 -> "3"
3. -> "3"
3.0 -> "3"
3.1 -> "3.1"
3.14 -> "3.14"
3.140 -> "3.14"

当前回答

OP希望删除多余的零,并使生成的字符串尽可能短。

我发现%g指数格式缩短了非常大和非常小的值的结果字符串。问题出现在不需要指数符号的值上,比如128.0,它既不是很大也不是很小。

这里有一种将数字格式化为短字符串的方法,仅在Decimal时使用%g指数表示法。Normalize创建的字符串太长。这可能不是最快的解决方案(因为它使用Decimal.normalize)

def floatToString (inputValue, precision = 3):
    rc = str(Decimal(inputValue).normalize())
    if 'E' in rc or len(rc) > 5:
        rc = '{0:.{1}g}'.format(inputValue, precision)        
    return rc

inputs = [128.0, 32768.0, 65536, 65536 * 2, 31.5, 1.000, 10.0]

outputs = [floatToString(i) for i in inputs]

print(outputs)

# ['128', '32768', '65536', '1.31e+05', '31.5', '1', '10']

其他回答

试试最简单、可能也是最有效的方法怎么样? normalize()方法删除所有最右边的尾随零。

from decimal import Decimal

print (Decimal('0.001000').normalize())
# Result: 0.001

适用于Python 2和Python 3。

——更新——

正如@BobStein-VisiBone指出的那样,唯一的问题是,像10,100,1000这样的数字……将以指数形式显示。使用下面的函数可以很容易地解决这个问题:

from decimal import Decimal


def format_float(f):
    d = Decimal(str(f));
    return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()

我呢,我会用('%f' % x).rstrip('0').rstrip('.')——保证定点格式,而不是科学符号,等等。是的,不像%g那么流畅和优雅,但是,它是有效的(而且我不知道如何强迫%g永远不使用科学符号;-)。

一个新的挑战者出现了。

def prettify_float(real: float, precision: int = 2) -> str:
    '''
    Prettify the passed floating-point number into a human-readable string,
    rounded and truncated to the passed number of decimal places.

    This converter prettifies floating-point numbers for human consumption,
    producing more readable results than the default :meth:`float.__str__`
    dunder method. Notably, this converter:

    * Strips all ignorable trailing zeroes and decimal points from this number
      (e.g., ``3`` rather than either ``3.`` or ``3.0``).
    * Rounds to the passed precision for perceptual uniformity.

    Parameters
    ----------
    real : float
        Arbitrary floating-point number to be prettified.
    precision : int, optional
        **Precision** (i.e., number of decimal places to round to). Defaults to
        a precision of 2 decimal places.

    Returns
    ----------
    str
        Human-readable string prettified from this floating-point number.

    Raises
    ----------
    ValueError
        If this precision is negative.
    '''

    # If this precision is negative, raise an exception.
    if precision < 0:
        raise ValueError(f'Negative precision {precision} unsupported.')
    # Else, this precision is non-negative.

    # String prettified from this floating-point number. In order:
    # * Coerce this number into a string rounded to this precision.
    # * Truncate all trailing zeroes from this string.
    # * Truncate any trailing decimal place if any from this string.
    result = f'{real:.{precision}f}'.rstrip('0').rstrip('.')

    # If rounding this string from a small negative number (e.g., "-0.001")
    # yielded the anomalous result of "-0", return "0" instead; else, return
    # this result as is.
    return '0' if result == '-0' else result

不要相信我的谎言

pytest风格的单元测试,否则就不会发生。

def test_prettify_float() -> None:
    '''
    Test usage of the :func:`prettify_float` prettifier.
    '''

    # Defer test-specific imports.
    from pytest import raises

    # Assert this function prettifies zero as expected.
    assert prettify_float(0.0) == '0'

    # Assert this function prettifies a negative integer as expected.
    assert prettify_float(-2.0) == '-2'

    # Assert this prettifier prettifies a small negative float as expected.
    assert prettify_float(-0.001) == '0'

    # Assert this prettifier prettifies a larger negative float as expected.
    assert prettify_float(-2.718281828) == '-2.72'
    assert prettify_float(-2.718281828, precision=4) == '-2.7183'

    # Assert this function prettifies a positive integer as expected.
    assert prettify_float(3.0) == '3'

    # Assert this function prettifies a positive float as expected.
    assert prettify_float(3.14159265359) == '3.14'
    assert prettify_float(3.14159265359, precision=4) == '3.1416'

    # Assert this prettifier raises the expected exception when passed a
    # negative precision.
    with raises(ValueError):
        prettify_float(2.718281828, precision=-2)

%100纯Python

忽略那些诱人的简单答案,比如:

琐碎的一行。它们在常见的边缘情况下都失败了,比如整数或小的负浮点数。 第三方包。NumPy, QuantiPhy和more_itertools?你肯定是在开玩笑。不要额外增加维护负担或代码债务。也就是说……

在prettify_float()上抛出@beartype,以增加运行时安全性,你就成功了!你的用户群会对你赞不绝口。那我也是,我很确定我的偏见在这里表现出来了。

另请参阅

这个答案站在巨大的猛犸象的肩膀上,包括:

亚历克斯·马尔泰利聪明的回答。 PolyMesh对Martelli答案的推广,以捕捉小负浮的边缘情况。 Kaushal Modi对PolyMesh的答案进行了概括,以强制实现小数点后两位的精度。

如果你想要一些既适用于数字输入又适用于字符串输入的东西(感谢@mike-placentra的bug搜索):

def num(s):
    """ 3.0 -> 3, 3.001000 -> 3.001 otherwise return s """
    s = str(s)
    try:
        int(float(s))
        if '.' not in s:
            s += '.0'
        return s.rstrip('0').rstrip('.')
    except ValueError:
        return s

>>> for n in [3, 3., 3.0, 3.1, 3.14, 3.140, 3.001000, 30 ]: print(num(n))
... 
3
3
3
3.1
3.14
3.14
3.001
30

>>> for n in [3, 3., 3.0, 3.1, 3.14, 3.140, 3.001000, 30 ]: print(num(str(n)))
... 
3
3
3
3.1
3.14
3.14
3.001
30

在看了几个类似问题的答案后,这似乎是我最好的解决方案:

def floatToString(inputValue):
    return ('%.15f' % inputValue).rstrip('0').rstrip('.')

我的推理:

%g并没有摆脱科学符号。

>>> '%g' % 0.000035
'3.5e-05'

小数点后15位似乎可以避免奇怪的行为,并且对我的需求有足够的精度。

>>> ('%.15f' % 1.35).rstrip('0').rstrip('.')
'1.35'
>>> ('%.16f' % 1.35).rstrip('0').rstrip('.')
'1.3500000000000001'

我可以使用format(inputValue, '.15f')。而不是'%。inputValue,但这有点慢(~30%)。

我本可以使用Decimal(inputValue).normalize(),但这也有一些问题。首先,它慢了很多(~11倍)。我还发现,尽管它具有相当高的精度,但在使用normalize()时,它仍然会遭受精度损失。

>>> Decimal('0.21000000000000000000000000006').normalize()
Decimal('0.2100000000000000000000000001')
>>> Decimal('0.21000000000000000000000000006')
Decimal('0.21000000000000000000000000006')

最重要的是,我仍然会从浮点数转换为十进制,这可能会让你得到其他东西,而不是你在那里输入的数字。我认为十进制工作最好时,算术保持在十进制和十进制初始化的字符串。

>>> Decimal(1.35)
Decimal('1.350000000000000088817841970012523233890533447265625')
>>> Decimal('1.35')
Decimal('1.35')

我确信Decimal.normalize()的精度问题可以使用上下文设置调整为所需的精度,但考虑到已经较慢的速度和不需要荒谬的精度,以及我仍然从浮点数转换并失去精度的事实,我认为不值得继续下去。

我不关心可能的“-0”结果,因为-0.0是一个有效的浮点数,它可能是一个罕见的发生,但既然你提到你想保持字符串结果尽可能短,你总是可以使用一个额外的条件在非常小的额外速度成本。

def floatToString(inputValue):
    result = ('%.15f' % inputValue).rstrip('0').rstrip('.')
    return '0' if result == '-0' else result