geew
V2EX  ›  问与答

[ Python ] 数字转字符串格式化问题, 不要科学记数法格式输出怎么搞

  •  
  •   geew · Apr 27, 2018 · 2413 views
    This topic created in 2961 days ago, the information mentioned may be changed or developed.
    举几个例子
    - 0.00000001000000000000 -> '0.00000001'
    - 90000 -> '90000'
    - 999999.000000001000 -> "999999.000000001"

    尝试了 '%.f', 转成 decimal 再转字符串都不行, 总会搞成科学记数法输出 有更好的方案吗
    3 replies    2018-07-10 20:55:34 +08:00
    goreliu
        1
    goreliu  
       Apr 27, 2018
    用正则表达式替换。
    glasslion
        2
    glasslion  
       Apr 27, 2018
    要指定截取的小数位数

    print("%.8f" % 0.00000001000000000000)

    print("%.9f" % 999999.000000001000)
    geew
        3
    geew  
    OP
       Jul 10, 2018
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1081 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 23:29 · PVG 07:29 · LAX 16:29 · JFK 19:29
    ♥ Do have faith in what you're doing.