大多数Pythonic方式打印*最多*一些小数位[重复]

时间:2021-10-03 22:54:29

This question already has an answer here:

这个问题在这里已有答案:

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.

我想格式化一个浮点数列表,最多可以说是2个小数位。但是,我不想尾随零,我不想要尾随小数点。

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

因此,例如,4.001 => 4,4.79 7 => 4.8,8.992 => 8.99,13.577 => 13.58。

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?

简单的解决方案是('%。2f'%f).rstrip('。0')('%。2f'%f).rstrip('0')。rstrip('。')。但是,这看起来很丑陋而且看起来很脆弱。任何更好的解决方案,可能有一些神奇的格式标志?

3 个解决方案

#1


15  

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

你需要分开0和。剥离;这样你就不会剥掉自然的0。

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

或者,使用format()函数,但这实际上归结为同样的事情:

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

Some tests:

一些测试:

>>> 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'

#2


13  

The g formatter limits the output to n significant digits, dropping trailing zeroes:

g格式化程序将输出限制为n位有效数字,删除尾随零:

>>> "{:.3g}".format(1.234)
'1.23'
>>> "{:.3g}".format(1.2)
'1.2'
>>> "{:.3g}".format(1)
'1'

#3


5  

In general working with String[s] can be slow. However, this is another solution:

通常使用String [s]可能很慢。但是,这是另一种解决方案:

>>> from decimal import Decimal
>>> precision = Decimal('.00')
>>> Decimal('4.001').quantize(precision).normalize()
Decimal('4')
>>> Decimal('4.797').quantize(precision).normalize()
Decimal('4.8')
>>> Decimal('8.992').quantize(precision).normalize()
Decimal('8.99')
>>> Decimal('13.577').quantize(precision).normalize()
Decimal('13.58')

You may find more info here: http://docs.python.org/2/library/decimal.html

您可以在这里找到更多信息:http://docs.python.org/2/library/decimal.html

#1


15  

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

你需要分开0和。剥离;这样你就不会剥掉自然的0。

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

或者,使用format()函数,但这实际上归结为同样的事情:

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

Some tests:

一些测试:

>>> 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'

#2


13  

The g formatter limits the output to n significant digits, dropping trailing zeroes:

g格式化程序将输出限制为n位有效数字,删除尾随零:

>>> "{:.3g}".format(1.234)
'1.23'
>>> "{:.3g}".format(1.2)
'1.2'
>>> "{:.3g}".format(1)
'1'

#3


5  

In general working with String[s] can be slow. However, this is another solution:

通常使用String [s]可能很慢。但是,这是另一种解决方案:

>>> from decimal import Decimal
>>> precision = Decimal('.00')
>>> Decimal('4.001').quantize(precision).normalize()
Decimal('4')
>>> Decimal('4.797').quantize(precision).normalize()
Decimal('4.8')
>>> Decimal('8.992').quantize(precision).normalize()
Decimal('8.99')
>>> Decimal('13.577').quantize(precision).normalize()
Decimal('13.58')

You may find more info here: http://docs.python.org/2/library/decimal.html

您可以在这里找到更多信息:http://docs.python.org/2/library/decimal.html