1

I have some code to convert string to float:

n = float('9400108205499941468492')

My unittest throws exceptions:

self.assertEqual(9400108205499941468492, n)

Expected :9400108205499941468492
Actual   :9.400108205499941e+21

What should I do to disable scientific notation in float().

2
  • 4
    These are equivalent. The way it is displayed is irrelevant. The unit test is failing because you are comparing an int to a float. Commented Aug 16, 2022 at 16:50
  • 1
    The problem isn't the scientific notation. The problem is that floats don't have enough precision to represent that number. Commented Aug 16, 2022 at 16:54

2 Answers 2

1

As Josh Friedlander mentions you are comparing an int to a float which will always be invalid.

In order to represent a float without scientific notation you can use Decimal but this is also not going to make your comparison to an int valid...

>>> from decimal import Decimal
>>> n = float('9400108205499941468492')
>>> format(Decimal.from_float(n))
'9400108205499941388288'
>>> y = format(Decimal.from_float(n))
>>> type(y)
<class 'str'>
Sign up to request clarification or add additional context in comments.

Comments

0

you can use int() instead of float and when you add 0.5 or any other float then if you want the approximate value then you use int

2 Comments

i believe 9400108205499941468492 is too big for int.
@topcan5 - Nope >>> x = 9400108205499941468492 >>> x 9400108205499941468492 >>> type(x) <class 'int'>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.