2024-06-26 02:14:51 发布
网友
是否有格式代码将-2.34格式化为'-2.3',但将+2.34格式化为'2.3'(注意前导空格)?基本上显示负号,但为正号留出空间
您可以在float上尝试format:
float
format
>>> "{: .1f}".format(+2.34) ' 2.3' >>> "{: .1f}".format(-2.34) '-2.3'
使用f字符串可以非常简洁地完成:
MYSTR = 2.34 print(f'{MYSTR:{".1f" if MYSTR < 0 else " .1f"}}')
txt = "The temperature is between {: } and {: } degrees celsius." print(txt.format(-3, 7))
答复:
The temperature is between -3 and 7 degrees celsius.
您可以在
float
上尝试format
:使用f字符串可以非常简洁地完成:
使用“”(空格)在正数前插入空格,在负数前插入减号:
答复:
相关问题 更多 >
编程相关推荐