






1. 使用 `float()` 函数:
python
my_int = 10
my_float = float(my_int)
2. 使用 `decimal.Decimal` 类:
python
import decimal
my_int = 10
my_decimal = decimal.Decimal(my_int) 转换为 Decimal 对象
my_float = float(my_decimal)
3. 使用 `numpy.float64()` 函数(如果您使用 NumPy 库):
```python
import numpy as np
my_int = 10
my_float = np.float64(my_int)
```
4. 使用 `fractions.Fraction` 类(如果您使用 Fraction 库):
```python
from fractions import Fraction
my_int = 10
my_fraction = Fraction(my_int) 转换为 Fraction 对象
my_float = float(my_fraction)
```
```python
def convert_int_to_float(integer):
"""Converts an integer to a floating point number.
Args:
integer: The integer to convert.
Returns:
The floating point number representation of the integer.
"""
return float(integer)
```
```python
使用 float() 函数
x = int(10)
y = float(x)
print(y) 输出:10.0
使用 decimal.Decimal() 类
import decimal
x = int(10)
y = decimal.Decimal(x)
print(y) 输出:Decimal('10.0')
```
```python
def to_float(x):
"""Converts an integer or string to a floating point number.
Args:
x: An integer or string.
Returns:
A float.
"""
if isinstance(x, int):
return float(x)
elif isinstance(x, str):
return float(x.replace(",", ""))
else:
raise ValueError("Unknown type: {}".format(type(x)))
```