[英]How do I limit the number of significant digits to *not more* than something with str.format() or f-strings?
我可以做点什么
'Correct answers: {:.2f}'.format(value)
对于3.141592的值,这将根据需要返回3.14。 但是,对于值0,它将返回0.00,如预期的那样但不是所希望的。 我希望在这种情况下看到“0”。 可能吗?
我想你正在寻找:
'Correct answers: {}'.format(round(5.666666, 2))
'Correct answers: {}'.format(round(5.6, 2))
'Correct answers: {}'.format(round(5, 2))
'Correct answers: {}'.format(round(0, 2))
输出:
Correct answers: 5.67
Correct answers: 5.6
Correct answers: 5
Correct answers: 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.