-1

Suppose I have this large number,x

3333333333333333333333333333333333333333333333333333333333333 (which I want)

and this large number,n 3333333333333333333333333333333333333333333333333333333333333.0.

When I convert n into an int through the round function, it provides me with a completely different number, which I suppose is because of floating point precision limitations.

Is there anyway to round of that number into a integer. I don't have to worry about any other decimal points, just .0.

  • 1
    Does this answer your question? [Python float to int conversion](https://stackoverflow.com/questions/6569528/python-float-to-int-conversion) – Mark Jun 19 '23 at 09:15
  • 8
    You *don't have* 3333333333333333333333333333333333333333333333333333333333333.0. You have 3333333333333333640373015225566689909307473186543079037861888.0. – mkrieger1 Jun 19 '23 at 09:18
  • @MarkHill, I was looking at changing a large number (more than 16 digits) thats in floating point without any following decimal except for the .0, into an integer whilst not sacrificing the length of the number. – About_to_give_up Jun 19 '23 at 09:19
  • 2
    Floats have limited storage space available. Just pasting that number into the interpreter you'll get `3.3333333333333336e+60`. That float never had that value to begin with, and of course converting it to an int won't make it any better. – deceze Jun 19 '23 at 09:19
  • If you store `n` as a float, you have already lost precision. So there's no way it can be reliably converted to your desired int. For example, `3333333333333333333333333333333333333333333333333333333333333.0 == 3333333333333333333333333333333333333333333333333333333333334.0` is `True` - two different integers correspond to the **same** float. – slothrop Jun 19 '23 at 09:19
  • @mkrieger1, I kow that, I wanted to know how I could work around that number to produce 3333333333333333333333333333333333333333333333333333333333333 this instead of 3333333333333333640373015225566689909307473186543079037861888.0. – About_to_give_up Jun 19 '23 at 09:20
  • The only way around that is to not use floats. – deceze Jun 19 '23 at 09:20
  • You should think of int -> float (for large numbers) like a lossy compression. If you convert a WAV to an MP3, you can't convert the MP3 perfectly back to the original WAV. – slothrop Jun 19 '23 at 09:20
  • Please show an actual [mre] code so that we know what you are dealing with exactly. – mkrieger1 Jun 19 '23 at 09:21
  • 2
    Try to construct a real example. What is the type of your starting value? Is it `float`? If so, then it cannot be `3333333333333333333333333333333333333333333333333333333333333.0`. You you want a more precise fixed point value, consider using `decimal.Decimal`, which *can* represent that number. Or consider using `fractions.Fraction`. Again, you need to specify the type of the starting value. – Tom Karzes Jun 19 '23 at 09:30
  • Tom's comment is the only real answer to this, though if your starting value _is_ a float, then how do you expect to reconstruct the data? What if it isn't just a lot of the same number? Technically this works for the _specific_ example you have, impossible to accurately reconstruct any other number though: `int(str(num)[0] * int(str(num).split('+')[1]))` – Peter Jun 19 '23 at 09:35

1 Answers1

0
from decimal import Decimal

print(int(Decimal("3333333333333333333333333333333333333333333333333333333333333.0")))

Output:

3333333333333333333333333333333333333333333333333333333333333
Michael Butscher
  • 10,028
  • 4
  • 24
  • 25