在科学计数法中使用固定指数

发布于 2024-12-18 01:53:06 字数 884 浏览 2 评论 0原文

考虑以下 Python 片段:

for ix in [0.02, 0.2, 2, 20, 200, 2000]:
    iss = str(ix) + "e9"
    isf = float(iss)
    print(iss + "\t=> " + ("%04.03e" % isf) + " (" + str(isf) + ")")

它生成以下输出:

0.02e9  => 2.000e+07 (20000000.0)
0.2e9   => 2.000e+08 (200000000.0)
2e9     => 2.000e+09 (2000000000.0)
20e9    => 2.000e+10 (20000000000.0)
200e9   => 2.000e+11 (2e+11)
2000e9  => 2.000e+12 (2e+12)

是否可以以某种方式“返回”?也就是说:

2.000e+07 => 0.02e9 
2.000e+08 => 0.2e9
2.000e+09 => 2e9    
2.000e+10 => 20e9   
2.000e+11 => 200e9  
2.000e+12 => 2000e9

...我指定我希望指数为e+09;然后无论我向这个假设函数输入什么数字,都会返回该指数中的数值?是否可以在每种情况下为整数部分和小数部分指定零填充? (即000.0200e9020.0000e9)?

Consider the following Python snippet:

for ix in [0.02, 0.2, 2, 20, 200, 2000]:
    iss = str(ix) + "e9"
    isf = float(iss)
    print(iss + "\t=> " + ("%04.03e" % isf) + " (" + str(isf) + ")")

It generates the following output:

0.02e9  => 2.000e+07 (20000000.0)
0.2e9   => 2.000e+08 (200000000.0)
2e9     => 2.000e+09 (2000000000.0)
20e9    => 2.000e+10 (20000000000.0)
200e9   => 2.000e+11 (2e+11)
2000e9  => 2.000e+12 (2e+12)

Is it possible to "go back" somehow? That is:

2.000e+07 => 0.02e9 
2.000e+08 => 0.2e9
2.000e+09 => 2e9    
2.000e+10 => 20e9   
2.000e+11 => 200e9  
2.000e+12 => 2000e9

... I'd specify I want the exponent to be e+09; and then whatever number I throw at this hypothetic function, returns the number value in that exponent? Would it be possible to specify zero padding for both the whole and the decimal part in each case? (i.e. 000.0200e9 and 020.0000e9)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独患者 2024-12-25 01:53:06

自己格式化(请参阅格式规范迷你语言 ):

for ix in [.02e9, .2e9, 2e9, 20e9, 200e9, 2000e9]:
    print('{:.3e} => {:0=8.3f}e9'.format(ix, ix / 1e9))

输出

2.000e+07 => 0000.020e9
2.000e+08 => 0000.200e9
2.000e+09 => 0002.000e9
2.000e+10 => 0020.000e9
2.000e+11 => 0200.000e9
2.000e+12 => 2000.000e9

解释

{:0=8.3f} 表示“零填充,符号和数字之间的填充,总字段宽度 8,小数点后 3 位,定点格式”。

Format it yourself (see Format Specification Mini-Language):

for ix in [.02e9, .2e9, 2e9, 20e9, 200e9, 2000e9]:
    print('{:.3e} => {:0=8.3f}e9'.format(ix, ix / 1e9))

Output

2.000e+07 => 0000.020e9
2.000e+08 => 0000.200e9
2.000e+09 => 0002.000e9
2.000e+10 => 0020.000e9
2.000e+11 => 0200.000e9
2.000e+12 => 2000.000e9

Explanation

{:0=8.3f} means "zero-pad, pad between the sign and the number, total field width 8, 3 places after the decimal, fixed point format".

稍尽春風 2024-12-25 01:53:06

啊好吧,明白了:

for ix in [0.02, 0.2, 2, 20, 200, 2000]:
  iss=str(ix) + "e9"
  isf=float(iss)
  isf2=isf/float("1e9")
  isf2s = ("%04.03f" % isf2) + "e9"
  print(iss + "\t=> " + ("%04.03e" % isf ) + " (" + str(isf) + ")" + " -> " + isf2s )

...给出:

0.02e9  => 2.000e+07 (20000000.0) -> 0.020e9
0.2e9   => 2.000e+08 (200000000.0) -> 0.200e9
2e9 => 2.000e+09 (2000000000.0) -> 2.000e9
20e9    => 2.000e+10 (20000000000.0) -> 20.000e9
200e9   => 2.000e+11 (2e+11) -> 200.000e9
2000e9  => 2.000e+12 (2e+12) -> 2000.000e9

抱歉发帖,
干杯!

Ah well, got it:

for ix in [0.02, 0.2, 2, 20, 200, 2000]:
  iss=str(ix) + "e9"
  isf=float(iss)
  isf2=isf/float("1e9")
  isf2s = ("%04.03f" % isf2) + "e9"
  print(iss + "\t=> " + ("%04.03e" % isf ) + " (" + str(isf) + ")" + " -> " + isf2s )

... gives:

0.02e9  => 2.000e+07 (20000000.0) -> 0.020e9
0.2e9   => 2.000e+08 (200000000.0) -> 0.200e9
2e9 => 2.000e+09 (2000000000.0) -> 2.000e9
20e9    => 2.000e+10 (20000000000.0) -> 20.000e9
200e9   => 2.000e+11 (2e+11) -> 200.000e9
2000e9  => 2.000e+12 (2e+12) -> 2000.000e9

Sorry for posting,
Cheers!

淡写薰衣草的香 2024-12-25 01:53:06

对于几年后仍然遇到的任何人...

您可以使用 python 的 Decimal 和 quantize

https://docs.python.org/3.6/library/decimal.html#decimal.Decimal.quantize

For anyone still coming across this years later...

You can use python's Decimal with quantize

https://docs.python.org/3.6/library/decimal.html#decimal.Decimal.quantize

久而酒知 2024-12-25 01:53:06

我编写了一个名为 sciform 的 PyPi 包,它支持这些和许多其他数字格式选项。

from sciform import Formatter

sform = Formatter(
    exp_mode="scientific",
    exp_val=9,
)

num_list = [2e7, 2e8, 2e9, 2e10, 2e11, 2e12]

for num in num_list:
    result = sform(num)
    print(f'{num:.3e} => {result}')

# 2.000e+07 => 0.02e+09
# 2.000e+08 => 0.2e+09
# 2.000e+09 => 2e+09
# 2.000e+10 => 20e+09
# 2.000e+11 => 200e+09
# 2.000e+12 => 2000e+09

我们还可以实现左右补零,这样所有格式化的字符串都有固定的长度。左填充选项控制左填充,右填充可以使用小数位舍入模式来完成。

from sciform import Formatter

sform = Formatter(
    exp_mode="scientific",
    exp_val=9,
    round_mode="dec_place",
    ndigits=4,  # Show 4 digits past the decimal place. ndigits can be negative.
    fill_char="0",  # Left pad with zeros
    left_pad_dec_place=3,  # left pad *the mantissa* to the 10^3 (thousands) place.
)

num_list = [2e7, 2e8, 2e9, 2e10, 2e11, 2e12]

for num in num_list:
    result = sform(num)
    print(f'{num:.3e} => {result}')

# 2.000e+07 => 0000.0200e+09
# 2.000e+08 => 0000.2000e+09
# 2.000e+09 => 0002.0000e+09
# 2.000e+10 => 0020.0000e+09
# 2.000e+11 => 0200.0000e+09
# 2.000e+12 => 2000.0000e+09

I've written a PyPi package called sciform that has support for these and many other numerical formatting options.

from sciform import Formatter

sform = Formatter(
    exp_mode="scientific",
    exp_val=9,
)

num_list = [2e7, 2e8, 2e9, 2e10, 2e11, 2e12]

for num in num_list:
    result = sform(num)
    print(f'{num:.3e} => {result}')

# 2.000e+07 => 0.02e+09
# 2.000e+08 => 0.2e+09
# 2.000e+09 => 2e+09
# 2.000e+10 => 20e+09
# 2.000e+11 => 200e+09
# 2.000e+12 => 2000e+09

We can also realize left and right zero padding so all the formatted strings have a fixed length. The left padding options control left padding, and right padding can be accomplished using the decimal place rounding mode.

from sciform import Formatter

sform = Formatter(
    exp_mode="scientific",
    exp_val=9,
    round_mode="dec_place",
    ndigits=4,  # Show 4 digits past the decimal place. ndigits can be negative.
    fill_char="0",  # Left pad with zeros
    left_pad_dec_place=3,  # left pad *the mantissa* to the 10^3 (thousands) place.
)

num_list = [2e7, 2e8, 2e9, 2e10, 2e11, 2e12]

for num in num_list:
    result = sform(num)
    print(f'{num:.3e} => {result}')

# 2.000e+07 => 0000.0200e+09
# 2.000e+08 => 0000.2000e+09
# 2.000e+09 => 0002.0000e+09
# 2.000e+10 => 0020.0000e+09
# 2.000e+11 => 0200.0000e+09
# 2.000e+12 => 2000.0000e+09
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文