python - 如何将有效数字的数量限制为 * 不超过 * 不超过 str.format() 或 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
推荐阅读
- rust - 如何使用循环在 Rust 中按顺序创建一个 &'static str 数组?
- android - 上传 Bundle 而不是 APK 时,Android 应用中的 TextView 看起来不同
- php - Swiftmailer 发送一条消息但不发送另一条消息
- node.js - Amazon Cognito - AdminCreateUser - 如何不跳过电子邮件验证
- sql - 查询多个 Azure SQL 数据库 - Azure SQL 数据库不能用作中央管理服务器
- r - 如何将文本文件中的特定值循环附加到 R 中预先指定对角线的矩阵中?
- r - 从 RC 类方法打印时未使用的参数错误
- r - 使用 dplyr 和条件格式从字符串构造常微分方程
- r - 我可以在没有 R studio 的情况下使用 R 吗?
- c# - 如何获取 html 输入按钮以正确显示 resource.resx 键/值条目?