Python Forum
Exponential Number Conversion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exponential Number Conversion
#1
Hello,

I am trying to pass 8.4e9 to a function to send to an instrument. However when I print this out it comes out in the wrong format (8.4000000e+06). How can I confirm it stays in the correct 8.4e9 format?

I can find how to convert from float to exponential etc, but I cannot find how to shift / format the exponential base number?

Thanks,
Tuurbo46
Reply
#2
You could post-process the result with the re module
>>> def replace(match):
...     return match.group(1) + (match.group(2) or '0')
... 
>>> import re
>>> x = 8.4e9
>>> s = f'{x:e}'
>>> re.sub(r'(e[+-])0*(\d*)$', replace, s)
'8.400000e+9'
or perhaps
>>> x = 8.4e9
>>> s = f'{x:e}'
>>> s
'8.400000e+09'
>>> L = re.split(r'(e[+-])0*(\d*)$', s)
>>> L[-2] = L[-2] or '0'
>>> ''.join(L)
'8.400000e+9'
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(Jan-08-2026, 11:12 AM)Tuurbo46 Wrote: I am trying to pass 8.4e9 to a function to send to an instrument. However when I print this out it comes out in the wrong format (8.4000000e+06).
I'm assuming that the +06 is really a +09 and the actual number isn't changing.

Python has some facility for changing the form of the mantissa.

>>> x = 8.4e9
>>> f"{x:e}"
'8.400000e+09'
>>> f"{x:.1e}"
'8.4e+09'
Unfortunately according to https://docs.python.org/3/library/string.html, the exponent always has at least two digits with this style of formatting. Also the sign is always present, even when positive. So if you need a single digit and to remove a positive sign indication, you would have to resort to string manipulation or a different exponential rendering.

>>> f"{x:.1e}".replace("+0","")
'8.4e9'
Reply
#4
I don't understand the problem. How is 9 being changed to 6?

Just working in Idle, I get:

x = 8.4e9
print(x) # returns 8400000000.0
Which is correct, if you count the columns up to the 8. Pass x to a function.

x = 8.4e9
def do_it(num):
    print(num)
    
do_it(x) # prints 8400000000.0
def do_it(num, power):
    print(num*10** power)

x = 8.4
power = 9
do_it(x, power)
Maybe you are using an old version of Python?
Reply
#5
def custom_scientiffic_notation(
    value: float, min_value: int | None = None, max_value: int | None = None
) -> str:
    """
    Convert a float to scientific notation with one decimal place.
    The sign of the exponent is stripped, if the value is positive.
    Leading 0 of the exponent is being stripped.

    Optionally a ValueError is risen, if min_value or max_value is given and value
    is out of range.

    A formatted str is returned.
    """
    if min_value is not None and value < min_value:
        raise ValueError("value is too small")
    if max_value is not None and value > max_value:
        raise ValueError("value is too big")

    v, e = f"{value:.1e}".split("e")
    return f"{v}e{int(e)}"
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to get non-exponential format Skaperen 1 2,521 Nov-21-2021, 08:51 PM
Last Post: bowlofred
  multiple number format conversion oli_action 4 4,283 Aug-11-2020, 05:10 AM
Last Post: perfringo
  stopping number conversion before end Skaperen 6 5,145 Jul-12-2020, 09:22 AM
Last Post: DeaD_EyE
  understanding exponential and bitwise operators srm 1 3,168 Jun-15-2019, 11:14 AM
Last Post: ThomasL
  Dealing with Exponential data parthi1705 11 14,876 May-30-2019, 10:16 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020