Let int accept scientific notation strings (with flag)

Same as Let int accept scientific notation strings, but with a flag.

More precisely, when sci=True is passed, let int accept a scientific notation string with (say) integer significand and non-negative integer exponent.

1 Like

After reading the discussion I had not read yet, I withdraw my previous support to adding this with a flag. Sorry.

3 Likes

I withdraw my previous support to adding this with a flag.

Why?

I don’t think this needs a standard function, you can do this in about 7 lines of code. The usual semantics of converting X.YeZ to integer would be to shift the decimal point in ...000X.Y000... by Z and call int() on the result.

Could you post an example?

>>> def intsci(s: str) -> int:
...     float(s)  # check format
...     flt, _, exp = s.lower().partition('e')
...     a, _, b = flt.partition('.')
...     x = int(exp or 0)
...     return int(a[:x] or 0) if x < 0 else int(a + str.ljust(b[:x], x, '0'))
... 
>>> intsci('1e23')
100000000000000000000000
>>> intsci('1.234e3')
1234
>>> intsci('5678.9e-3')
5
>>> intsci('1.2.3')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in intsci
ValueError: could not convert string to float: '1.2.3'

We expect a certain level of effort and quality both in the initial idea proposal as well as the subsequent discussion. Based on the activity here and in the previous topic, the answer appears to be “no”, in various ways.