简体   繁体   English

十进制格式问题python 3.0

[英]Decimal Formatting issue python 3.0

Minor formatting issue: Im having trouble dealing with these kinda values: 次要格式问题:我在处理以下种类值时遇到麻烦:

97 $ 7.922816251426434e+26 
98 $ 1.5845632502852868e+27 
99 $ 3.1691265005705736e+27 
100 $ 6.338253001141147e+27

I want to make it print normally(?) with just 2 decimal places and the full number not the e+27 in the end.. 我想让它正常打印(?),仅保留小数点后2位,并以数字结尾而不是e + 27。

Thanks for your help in advance! 谢谢您的帮助!

Try string formatting 尝试字符串格式

"%.2f" % value

Since python 3, you may as well use string.format : 从python 3开始,您也可以使用string.format

"{:.2f}".format(value)

which looks more complicated, but is more modern and more powerful and worth getting used to ;) 它看起来更复杂,但是更现代,更强大,值得习惯;)

Try using format : 尝试使用format

' {0:.2f}'.format(num) ' {0:.2f}'.format(num)

Explanation: 说明:

{0} tells format to print the first argument -- in this case, num . {0}告诉格式输出第一个参数-在这种情况下为num

Everything after the colon (:) specifies the format_spec. colon (:)之后的所有内容均指定format_spec。

f Fixed point. f定点。 Displays the number as a fixed-point number. 将数字显示为定点数字。 The default precision is 6. 默认精度为6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM