在Python中,有一个最大长度的浮点数转字符串格式化参数吗?

huangapple go评论60阅读模式
英文:

In Python, is there a max length float to string formatting parameter?

问题

我有一个在控制台打印矩阵的程序,这意味着我希望每个矩阵条目的间距都相同,无论数字有多大,正数还是负数。我希望不需要显示为指数的数字不以指数形式显示,没有非零小数的浮点数显示为没有小数。我正在使用以下格式规则格式化任何浮点数 number

"{: 8.3g}".format(number)

然而,例外情况是非常小的数字显示为占用 9 个字符。例如:

("{: 8.3g}").format(-0.000000000000000000111)

返回 -1.11e-19

我怀疑它没有计算指数前面的减号,但可能是错误的。有没有办法更改格式规则以满足我的要求?

英文:

I have a program that prints matrices in the console which means I want the spacing for each matrix entry to be the same, regardless of how big the number is, positive or negative. I want numbers that do not need to be displayed as exponentials to not be displayed as such and floats with no non-zero decimals to be displayed without decimals. I am formatting any float number with the formatting rule:

"{: 8.3g}".format(number)

This formatting rule displays all numbers to take up exactly 8 characters, along with my preferences related to decimals and eponentials.

However, the exception to this is that very small numbers are displayed to take up 9 characters. For example:

("{: 8.3g}").format(-0.000000000000000000111) returns -1.11e-19.

I suspect it doesn't count the minus in front of the exponent but could be wrong. Any idea how to change the formatting rule to meet my requirements?

答案1

得分: 1

width参数是最小宽度,但如果你的指数有两位数,你需要一个宽度为9,以按照规定显示具有三个有效数字的数字。

-x.xxe-xx
123456789

如果你想要数字对齐,给一个width参数,可以容纳所有你的情况:

def f(x):
    result = f"{x: 8.3g}"
    print(f"{len(result)}: '{result}'")

def g(x):
    result = f"{x: 9.3g}"
    print(f"{len(result)}: '{result}'")

def h(x):
    result = f"{x: 10.3g}"
    print(f"{len(result)}: '{result}'")

f(1)
f(-1.11e-5)
f(1.11e-10)
f(-1.11e-100)
print()
g(1)
g(-1.11e-5)
g(1.11e-10)
g(-1.11e-100)
print()
h(1)
h(-1.11e-5)
h(1.11e-10)
h(-1.11e-100)

输出:

8: '       1'
9: '-1.11e-05'
9: ' 1.11e-10'
10: '-1.11e-100'

9: '        1'
9: '-1.11e-05'
9: ' 1.11e-10'
10: '-1.11e-100'

10: '         1'
10: ' -1.11e-05'
10: '  1.11e-10'
10: '-1.11e-100'

这里不是符号的问题。你已经通过在格式字符串中使用空格来处理了符号。

英文:

The width parameter is the minimal width, but if your exponent has two digits, you need a width of 9 to display the number with three significant digits as specified.

-x.xxe-xx
123456789

If you want the numbers aligned, give a width parameter that can host all your cases:

def f(x):
    result = f"{x: 8.3g}"
    print(f"{len(result)}: '{result}'")

def g(x):
    result = f"{x: 9.3g}"
    print(f"{len(result)}: '{result}'")

def h(x):
    result = f"{x: 10.3g}"
    print(f"{len(result)}: '{result}'")

f(1)
f(-1.11e-5)
f(1.11e-10)
f(-1.11e-100)
print()
g(1)
g(-1.11e-5)
g(1.11e-10)
g(-1.11e-100)
print()
h(1)
h(-1.11e-5)
h(1.11e-10)
h(-1.11e-100)

Output:

8: '       1'
9: '-1.11e-05'
9: ' 1.11e-10'
10: '-1.11e-100'

9: '        1'
9: '-1.11e-05'
9: ' 1.11e-10'
10: '-1.11e-100'

10: '         1'
10: ' -1.11e-05'
10: '  1.11e-10'
10: '-1.11e-100'

The sign is not to blame here. You already took care of that by using the space in the format string.

huangapple
  • 本文由 发表于 2023年5月13日 22:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定