python保留小數位的方法


format函數

def memissue():
    print('內存信息:')
    mem = psutil.virtual_memory()
    # 單位換算為GB
    memtotal = mem.total / 1024 / 1024 / 1024
    memused = mem.used / 1024 / 1024 / 1024
    membaifen = mem.used / mem.total * 100
    print('總內存{:.2f}GB'.format(memtotal))
    print('已使用{:.2f}GB'.format(memused))
    print('內存使用率為:{}%'.format(format(membaifen, '.2f')))

輸出:

內存信息:
總內存15.87GB
已使用7.70GB
內存使用率為:48.54%

'%.2f' %a

def cuplist():
    print('磁盤信息:')
    disk = psutil.disk_partitions()
    diskuse = psutil.disk_usage('/')
    #單位換算為GB
    diskused = diskuse.used / 1024 / 1024 / 1024
    disktotal = diskuse.total / 1024 / 1024 / 1024
    diskbaifen = diskused / disktotal * 100
    print('%.2fGB' % diskused)
    print('%.2fGB' % disktotal)
    print('%.2f' % diskbaifen)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM