在Python中,要保留数字的小数位数,你可以使用 `round()` 函数或者字符串格式化方法。以下是两种方法的示例:
使用 `round()` 函数
```python
num = 3.9793
rounded_num = round(num, 6)
print(rounded_num) 输出:3.
使用字符串格式化
```python
num = 3.9793
formatted_num = "%.6f" % num
print(formatted_num) 输出:3.
两种方法都可以将数字 `num` 保留到小数点后6位。`round()` 函数会四舍五入到最接近的指定小数位数,而字符串格式化方法会直接格式化输出。