Python编程:怎么使用分数的__round__()方法

2024-10-14 07:42:26

1、首先在PyCharm软件中,打开一个Python项目。

Python编程:怎么使用分数的__round__()方法

2、在Python项目中,新建并打开一个空白的python文件(比如:test.py)。

Python编程:怎么使用分数的__round__()方法

3、在python文件编辑区中,输入:“from fractions import Fraction”,导入 fractions 模块的 Fraction 类型。

Python编程:怎么使用分数的__round__()方法

4、接着输入:“FX = Fraction('2.4')”,点击Enter键。

Python编程:怎么使用分数的__round__()方法

5、再输入:“roundX = FX.__round__()”,点击Enter键。

Python编程:怎么使用分数的__round__()方法

6、然后输入:“print(roundX)”,打印出相关数据结果。

Python编程:怎么使用分数的__round__()方法

7、在编辑区域点击鼠标右键,在弹出菜单中选择“运行”选项。

Python编程:怎么使用分数的__round__()方法

8、在运行结果窗口中查看运行结果,可以看到已经成功地使用分数的__round__()方法。

Python编程:怎么使用分数的__round__()方法
猜你喜欢