zoukankan      html  css  js  c++  java
  • Python之数字的格式化输出

    需求:

    将数字格式化后输出,并控制数字的位数、对齐、千位分隔符和其他的细节
    x = 1234.56789
    # Two decimal places of accuracy
    print(format(x, '0.2f')) # '1234.57'
    
    # Right justified in 10 chars, one-digit accuracy
    print(format(x, '>10.1f'))  # '1234.6'
    
    # Left justified
    print(format(x, '<10.1f'))  # '1234.6    '
    
    # Centered
    print(format(x, '^10.1f'))  # 1234.6
    
    # Inclusion of thousands separator
    print(format(x, ','))  # '1,234.56789'
    print(format(x, '0,.1f'))  # '1,234.6'
    二八十六进制整数
    x=1234
    print(bin(x))  # 0b10011010010
    print(oct(x))  # 0o2322
    print(hex(x))  # 0x4d2
    # 去掉进制的前缀,可以用format
    print(format(x,"b"))  # 10011010010
    print(format(x,"o"))  # 2322
    print(format(x,"x"))  # 4d2
  • 相关阅读:
    Spring Security配置logout地址
    flex布局
    视口的学习笔记
    box-sizing属性
    css清除浮动
    line-height的理解
    position和float小结
    css居中方法小结
    margin重叠
    浅谈负margin
  • 原文地址:https://www.cnblogs.com/zzy-9318/p/10461951.html
Copyright © 2011-2022 走看看