-2

I have a float,

5.8307200000000005e-06

But I only want the precision of 5 on it, so it looks like this

5.83072e-06

How can I do this in Python?

Update, new code

def precision(number):
    # The number you want to change the precision of
    number

    # Convert the number to scientific notation
    sci_notation = '{:.5e}'.format(number)

    # Split the scientific notation string into its coefficient and 
exponent parts
    coefficient, exponent = sci_notation.split('e')

    # Round the coefficient to 5 decimal places
    rounded_coefficient = round(float(coefficient), 5)

    # Rebuild the scientific notation string using the rounded 
coefficient and the original exponent
rounded_sci_notation = f'{rounded_coefficient}e{exponent}'

    # Convert the scientific notation string back to a float
    rounded_number = float(rounded_sci_notation)
    return rounded_number
TullMesh
  • 1
  • 1
  • 5
    `float` objects have a fixed *precision*. You seem to simply care about how the object is *printed to the screen*, correct? – juanpa.arrivillaga Dec 20 '22 at 15:54
  • No, I need the object in a list, so not only printed. Also, I want a precision of 5 on all my floats, printing "round(5.8307200000000005e-06, 12)" wont work. I tried the Decimal module but was unsuccessful. – TullMesh Dec 20 '22 at 16:01
  • @TullMesh I already explained to you **floats have a fixed precision**. the decimal reprsentation you see printed to the screen is only ever an approximation anyway, floats are binary represenations. Also, "No, I need the object in a list, so not only printed" makes absolutely no sense. What does "I need the object in a list" have to do with "not only printed". Those two things aren't inherently related at all. – juanpa.arrivillaga Dec 20 '22 at 16:08
  • @juanpa.arrivillaga I don't understand what you mean with "floats have a fixed precision", either. Looks like they have the float `5.8307200000000005e-06` and simply want the float `5.83072e-06`... – Kelly Bundy Dec 20 '22 at 16:13
  • @Tomerikoo That other question isn't about *significant* digits. How do you apply that here? – Kelly Bundy Dec 20 '22 at 16:15
  • @KellyBundy would you say the floats `5.83072e-06` and `5.8307200000000000e-06` have the same precision? – juanpa.arrivillaga Dec 20 '22 at 16:24
  • @juanpa.arrivillaga I don't know, depends on what you mean with that. – Kelly Bundy Dec 20 '22 at 16:25
  • @KellyBundy so, the *precision* of a floating point number typically refers to the number of significand bits, which is the same *for all floats* – juanpa.arrivillaga Dec 20 '22 at 16:27
  • @juanpa.arrivillaga I'd say of *almost* all (since subnormals have less, don't they?). Anyway, apparently the OP means the precision in terms of decimal digits of the default decimal representation. The question seems clear enough to me. – Kelly Bundy Dec 20 '22 at 16:33

1 Answers1

1

Here is how you format a number with 5 significant digits and scientific notation:

number = 5.8307200000000005e-06
print(f"{number:.5e}")
>>> 5.83072e-06

But this does nothing with the precision of the number, just the way it is printed.....

Daniel Schneider
  • 1,797
  • 7
  • 20
  • use the `decimal` package – Gábor Fekete Dec 20 '22 at 16:10
  • 1
    @TullMesh **you cannot change the precision of a `float`, it is a fixed-precision binary floating point format**. This is a very fundamental thing to understand – juanpa.arrivillaga Dec 20 '22 at 16:11
  • @TullMesh and also very crucial to understand, *the decimal representation you see printed doesn't really tell you the precision*. You are talking about "mathematical" precision. In which case, you just need to round. – juanpa.arrivillaga Dec 20 '22 at 16:13