代码之家  ›  专栏  ›  技术社区  ›  Algorias

正在从浮动中删除尾部“.0”

  •  38
  • Algorias  · 技术社区  · 17 年前

    输入数据是浮点和字符串的混合体。期望输出:

    0.0-->'0'

    0.1-->'0.1'

    我提出了以下生成器表达式,但不知是否有更快的方法:

    (str(i).rstrip('.0') if i else '0' for i in lst)
    

    编辑:目前我可以接受的解决方案是:

    ('%d'%i if i == int(i) else '%s'%i for i in lst)
    

    16 回复  |  直到 17 年前
        1
  •  86
  •   Ben Thayer ebas    5 年前

    看见 PEP 3101 :

    'g' - General format. This prints the number as a fixed-point
          number, unless the number is too large, in which case
          it switches to 'e' exponent notation.
    

    >>> "%g" % float(10)
    '10'
    

    >>> '{0:g}'.format(float(21))
    '21'
    

    新样式3.6+:

    >>> f'{float(21):g}'
    '21'
    
        2
  •  18
  •   dF.    17 年前

    rstrip 任何

    >>> '30000.0'.rstrip('.0')
    '3'
    

    实际上,只是 '%g' % i

    自从 str(i) 使用12位有效数字,我认为这将起作用:

    >>> numbers = [ 0.0, 1.0, 0.1, 123456.7 ]
    >>> ['%.12g' % n for n in numbers]
    ['1', '0', '0.1', '123456.7']
    
        3
  •  9
  •   user13876 user13876    17 年前
    >>> x = '1.0'
    >>> int(float(x))
    1
    >>> x = 1
    >>> int(float(x))
    1
    
        4
  •  8
  •   Mike Samuel    17 年前
    (str(i)[-2:] == '.0' and str(i)[:-2] or str(i) for i in ...)
    
        5
  •  8
  •   J.T. Hurley    17 年前
    def floatstrip(x):
        if x == int(x):
            return str(int(x))
        else:
            return str(x)
    

    不过,请注意,在我的系统0.100000000000001上,Python将0.1表示为不精确的浮点。

        6
  •  8
  •   jeromej    12 年前

    外面有这么多丑陋的东西

    float 这不需要成为一个 浮动 int 0

    (int(i) if i.is_integer() else i for i in lst)

    然后你就可以正常打印了。

        7
  •  7
  •   jfs    13 年前

    float 将整数值作为 int

    format = "%d" if f.is_integer() else "%s"
    print(format % f)
    

    Example

                 0.0 -> 0
                 0.1 -> 0.1
                10.0 -> 10
          12345678.9 -> 12345678.9
         123456789.0 -> 123456789
    12345678912345.0 -> 12345678912345
    12345678912345.6 -> 1.23456789123e+13
      1.000000000001 -> 1.0
    
        8
  •  4
  •   Robert Gamble    17 年前

    如果只关心精度的小数点后1位(如示例中所示),则可以执行以下操作:

    ("%.1f" % i).replace(".0", "")
    

    这会将数字转换为小数点后1位的字符串,如果是零,则将其删除:

    >>> ("%.1f" % 0).replace(".0", "")
    '0'
    >>> ("%.1f" % 0.0).replace(".0", "")
    '0'
    >>> ("%.1f" % 0.1).replace(".0", "")
    '0.1'
    >>> ("%.1f" % 1.0).replace(".0", "")
    '1'
    >>> ("%.1f" % 3000.0).replace(".0", "")
    '3000'
    >>> ("%.1f" % 1.0000001).replace(".0", "")
    '1'
    
        9
  •  3
  •   Andy Hume    17 年前
    from decimal import Decimal
    '%g' % (Decimal(str(x)))
    
        10
  •  2
  •   Daniel Naab    17 年前

    使用Python的 string formatting (使用 str.format() 使用Python 3.0时):

    from decimal import Decimal
    
    def format_number(i):
        return '%g' % (Decimal(str(i)))
    
        11
  •  2
  •   Ovidiu Dolha    9 年前

    以下代码将按原样转换变量no的内容,即。 45.60 . 如果你使用 str 45.6

    no = 45.60
    
    strNo = "%.2f" %no
    
        12
  •  1
  •   Holden Waterhouse    16 年前
    str(x)[-2:] == '.0' and int(x) or x
    
        13
  •  0
  •   A. Coady    17 年前
    >>> '%g' % 0
    '0'
    >>> '%g' % 0.0
    '0'
    >>> '%g' % 0.1
    '0.1'
    >>> '%g' % 1.0
    '1'
    
        14
  •  0
  •   niklauzg    9 年前

    FWIW,带Jinja2,其中var=10.3

    {{ var|round|int }} 将发出整数 10

    round(method='floor') round(method='ceil')

    {{ var|round(method='floor')|int }} 仍将发出整数 10

    precision=0 round 作用

    裁判: http://jinja.pocoo.org/docs/dev/templates/#round

        15
  •  0
  •   Edison    9 年前

    def remove_zeros(num):
        nums = list(num)
        indexes = (list(reversed(range(len(nums)))))
        for i in indexes:
            if nums[i] == '0':
                del nums[-1]
            else:
                break
        return "".join(nums)
    
    num = "0.000000363000"
    print(remove_zeros(num))
    

    印刷品:

        0.000000363
    
        16
  •  -2
  •   Charlie Martin    17 年前

    请使用0精度,如果需要,请添加句点。例如“%.0f。”

    >>> print "%.0f."%1.0
    1.
    >>>