lichun
V2EX  ›  问与答

按百分比计算占比,遇到无限小数都是怎么处理的?

  •  
  •   lichun · Feb 2, 2016 · 2159 views
    This topic created in 3789 days ago, the information mentioned may be changed or developed.

    code:

    In [1]: total = 11
    
    In [2]: a, b, c = 3, 3, 5
    
    In [3]: list(map(lambda x: x / total, [a, b, c]))
    Out[3]: [0.2727272727272727, 0.2727272727272727, 0.45454545454545453]
    
    In [4]: sum(map(lambda x: Decimal(x / total), [a, b, c]))
    Out[4]: Decimal('0.9999999999999999444888487687')
    

    怎么处理最后的占比结果

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2720 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 04:07 · PVG 12:07 · LAX 21:07 · JFK 00:07
    ♥ Do have faith in what you're doing.