2

I have a float and would like to limit to just two decimals.

I've tried format(), and round(), and still just get 0, or 0.0

x = 8.972990688205408e-05
print ("x: ", x)
print ("x using round():", round(x))
print ("x using format():"+"{:.2f}".format(x))

output:
x:  8.972990688205408e-05
x using round(): 0
x using format():0.00

I'm expecting 8.98, or 8.97 depending on what method used. What am I missing?

funk101
  • 163
  • 4
  • 13

4 Answers4

2

You are using the scientific notation. As glhr pointed out in the comments, you are trying to round 8.972990688205408e-05 = 0.00008972990688205408. This means trying to round as type float will only print the first two 0s after the decimal points, resulting in 0.00. You will have to format via 0:.2e:

x = 8.972990688205408e-05
print("{0:.2e}".format(x))

This prints:

8.97e-05

You asked in one of your comments on how to get only the 8.97. This is the way to do it:

y = x*1e+05
print("{0:.2f}".format(y))

output:

8.97
pr0f3ss
  • 527
  • 1
  • 4
  • 17
1

If I understand correctly, you only want to round the mantissa/significand? If you want to keep x as a float and output a float, just specify the precision when calling round:

x = round(8.972990688205408e-05,7)

Output:

8.97e-05 

However, I recommend converting x with the decimal module first, which "provides support for fast correctly-rounded decimal floating point arithmetic" (see this answer):

from decimal import Decimal
x = Decimal('8.972990688205408e-05').quantize(Decimal('1e-7')) # output: 0.0000897
print('%.2E' % x)

Output:

8.97E-05

Or use the short form of the format method, which gives the same output:

print(f"{x:.2E}")
glhr
  • 4,439
  • 1
  • 15
  • 26
1

In python (and many other programming language), any number suffix with an e with a number, it is power of 10 with the number.

For example

8.9729e05 = 8.9729 x 10^3 = 8972.9
8.9729e-05 = 8.9729 x 10^-3 = 0.000089729
8.9729e0 = 8.9729 x 10^0 = 8.9729
8.972990688205408e-05 8.972990688205408 x 10^-5 = 0.00008972990688205408
8.9729e  # invalid syntax

As pointed out by other answer, if you want to print out the exponential round up, you need to use the correct Python string format, you have many choices to choose from. i.e.

e   Floating point exponential format (lowercase, precision default to 6 digit) 
e   Floating point exponential format (uppercase, precision default to 6 digit).
g   Same as "e" if exponent is greater than -4 or less than precision, "f" otherwise
G   Same as "E" if exponent is greater than -4 or less than precision, "F" otherwise

e.g.

x = 8.972990688205408e-05
print('{:e}'.format(x))   # 8.972991e-05
print('{:E}'.format(x))   # 8.972991E-05
print('{:.2e}'.format(x))   # 8.97e-05

(Update)

OP asked a way to remove the exponent "E" number. Since str.format() or "%" notation just output a string object, break the "e" notation out of the string will do the trick.

'{:.2e}'.format(x).split("e")  # ['8.97', '-05']
print('{:.2e}'.format(x).split('e')[0])  # 8.97
mootmoot
  • 12,845
  • 5
  • 47
  • 44
  • Ok, so using ```print('{:.2e}'.format(x) # 8.97e-05``` how does one get rid of the ```e-05```? I just need 8.97. – funk101 Apr 05 '19 at 08:11
  • @funk101 split() is python 101. ;) – mootmoot Apr 05 '19 at 08:59
  • Yeah, I get it, I thought there would be a simple function that would do that, rather than split(). – funk101 Apr 05 '19 at 16:06
  • @funk101 Though Exponential handling is pretty standard across all programming language, I don't think stripping the `e` number is a general practice. – mootmoot Apr 08 '19 at 07:23
0
rount() returns closest multiple of 10 to the power minus ndigits, 

so there is no chance you will get 8.98 or 8.97. you can check here also.