Python,要打印精确格式的浮点数+00.00 [英] Python, want to print float in exact format +-00.00

查看:322
本文介绍了Python,要打印精确格式的浮点数+00.00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要格式化一个浮点格式+ -00.00,试图基本的字符串格式,但不能得到领先的+或 - 符号或两个前导0如果该值是小数,任何指针? b $ b

解决方案使用'%+ 06.2f'来适当地设置宽度和精度。格式(n)(或'{0:+ 06.2f} }'如果您的Python版本需要位置组件)。


I need to format a float to the format +-00.00, tried the basic string formatting but can't get the leading + or - sign or two leading 0s if the value is fractional, any pointers?

解决方案

Use '%+06.2f' to set the width and precision appropriately. The equivalent using new-style format strings is '{:+06.2f}'.format(n) (or '{0:+06.2f}' if your version of Python requires the positional component).

这篇关于Python,要打印精确格式的浮点数+00.00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆