【问题标题】:The second root of the number is up to 4 decimal places [duplicate]数字的第二个根最多为小数点后4位[重复]
【发布时间】:2018-05-13 08:00:09
【问题描述】:

我打算打印用户打算在输出中打印最多 4 位小数的第四位数 如何在不使用 Round() 函数的情况下获得最多四位小数?

import math 
n = int(input('Do you want to calculate the root of a few numbers? --> '))

for i in range(n):
    usrinp = int(input('Enter the number for root --> '))
    x = math.sqrt(usrinp)

    print(x)

例如:

你想计算几个数字的根吗? --> 4

输入根号 --> 1

输入根号 --> 2

输入根号 --> 3

输入根号 --> 19


输出:

1.0000

1.4142

1.7320

4.3588

【问题讨论】:

  • 请在您的问题中添加一些示例输入和预期输出。并告诉我们您的代码有什么问题?
  • 查找字符串格式迷你语言:docs.python.org/3/library/string.html#formatstrings
  • 关于您的要求,我已经编辑了我的问题。
  • 我是 Python 的初学者,完全使用我的所有功能对我来说有点困难。

标签: python python-3.x


【解决方案1】:

不使用round函数获得小数点后4位:

print('{:0.4f}'.format(x))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多