yoo
V2EX  ›  问与答

Python中如何获得小数点位数?

  •  
  •   yoo · Oct 23, 2012 · 10720 views
    This topic created in 5126 days ago, the information mentioned may be changed or developed.
    如题,计算会得到多个结果,如何获取结果的小数点后的位数,将符合位数的结果输出。
    3 replies  •  1970-01-01 08:00:00 +08:00
    liuxurong
    liuxurong
    Oct 23, 2012
    1
    round?
    paloalto
    paloalto
    Oct 23, 2012
    2
    循环得到的结果,然后用正则匹配出符合你要求位数的那个结果。

    ps:round是用来限定位数的吧?

    round(1/3.0,2)
    --->0.33000000002
    print round(1/3.0,2)
    --->0.33
    rexren
    rexren
    Oct 24, 2012
    3
    用round限定你需要的位数 然后和原数作比较。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1286 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:49 · PVG 05:49 · LAX 13:49 · JFK 16:49
    ♥ Do have faith in what you're doing.