在Python中,要输出一个浮点数的小数部分,你可以使用 `round` 函数或者格式化字符串。以下是两种方法的示例:
方法1:使用 `round` 函数
import mathpi = math.pi使用 round 函数保留两位小数rounded_pi = round(pi, 2)print(rounded_pi) 输出结果为:3.14

方法2:使用格式化字符串
pi = math.pi使用格式化字符串保留两位小数formatted_pi = "{:.2f}".format(pi)print(formatted_pi) 输出结果为:3.14
在这两种方法中,`round` 函数和格式化字符串都可以指定小数点后的位数,从而只输出小数部分。
