1. 使用 `round()` 函数:
```python
number = 3.
rounded_number = round(number, 1)
print(rounded_number) 输出:3.1
2. 使用字符串格式化(`format()` 函数):
```python
number = 3.
formatted_number = "{:.1f}".format(number)
print(formatted_number) 输出:3.1
3. 使用 `decimal` 模块的 `quantize` 函数:
```python
from decimal import Decimal
number = Decimal('3.')
quantized_number = number.quantize(Decimal('0.1'))
print(quantized_number) 输出:3.1
4. 使用 `numpy` 库的 `round` 函数:
```python
import numpy as np
number = np.array([3.])
rounded_number = np.round(number, 1)
print(rounded_number) 输出:[3.1]
以上方法都可以实现保留一位小数。选择哪一种方法取决于你的具体需求和偏好