久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

大多數(shù)Pythonic方式打印*最多*一些小數(shù)位

Most Pythonic way to print *at most* some number of decimal places(大多數(shù)Pythonic方式打印*最多*一些小數(shù)位)
本文介紹了大多數(shù)Pythonic方式打印*最多*一些小數(shù)位的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想格式化最多包含 2 個(gè)小數(shù)位的浮點(diǎn)數(shù)列表.但是,我不想要尾隨零,也不想要尾隨小數(shù)點(diǎn).

I want to format a list of floating-point numbers with at most, say, 2 decimal places. But, I don't want trailing zeros, and I don't want trailing decimal points.

例如,4.001 => 4, 4.797 => 4.8, 8.992 => 8.99, 13.577 => 13.58.

So, for example, 4.001 => 4, 4.797 => 4.8, 8.992 => 8.99, 13.577 => 13.58.

簡(jiǎn)單的解決方案是('%.2f' % f).rstrip('.0')('%.2f' % f).rstrip('0').rstrip('.').但是,這看起來(lái)相當(dāng)丑陋,而且似乎很脆弱.任何更好的解決方案,也許有一些神奇的格式標(biāo)志?

The simple solution is ('%.2f' % f).rstrip('.0')('%.2f' % f).rstrip('0').rstrip('.'). But, that looks rather ugly and seems fragile. Any nicer solutions, maybe with some magical format flags?

推薦答案

需要將0.分開剝離;這樣你就永遠(yuǎn)不會(huì)剝離自然的 0.

You need to separate the 0 and the . stripping; that way you won't ever strip away the natural 0.

或者,使用 format() 函數(shù),但這實(shí)際上歸結(jié)為同一件事:

Alternatively, use the format() function, but that really comes down to the same thing:

format(f, '.2f').rstrip('0').rstrip('.')

一些測(cè)試:

>>> def formatted(f): return format(f, '.2f').rstrip('0').rstrip('.')
... 
>>> formatted(0.0)
'0'
>>> formatted(4.797)
'4.8'
>>> formatted(4.001)
'4'
>>> formatted(13.577)
'13.58'
>>> formatted(0.000000000000000000001)
'0'
>>> formatted(10000000000)
'10000000000'

這篇關(guān)于大多數(shù)Pythonic方式打印*最多*一些小數(shù)位的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 欧洲一区二区三区 | 超碰在线观看97 | 中文字幕精品一区久久久久 | 亚洲欧美在线播放 | 一级特黄aaaaaa大片 | 国产永久免费视频 | 黄色免费观看网站 | 亚洲精品久久久久 | 国产精品偷乱一区二区三区 | 人人看人人草 | 国产激情一区二区三区 | 色多多视频在线观看 | 国产在线日韩 | 99一区二区三区 | 国产91丝袜在线播放 | 日韩精品极品 | 夜夜操夜夜 | 黑丝一区| 国产一区二区视频在线观看 | 亚洲永久免费视频 | 亚洲国产精品久久久久久 | 日本色视频 | 三级av片 | 久久国产精| 久久久久久国产精品 | 欧美福利在线观看 | 国产精品自拍一区 | 人人爱人人插 | 糖心vlog精品一区二区 | 亚洲午夜视频在线观看 | 黄av在线| 成人一区二区视频 | 欧美vieox另类极品 | 亚洲激情在线视频 | 精品网站999www | 日本成人一区二区三区 | 日韩av免费在线观看 | 亚洲一区欧美一区 | 欧美一级精品 | 成人理论影院 | 中文字幕第一页在线 |