-1

I want to return a big number in a plain format but instead I'm getting this:

>>> x=1000000000000000000000000000000
>>> int(1*x/100) # try to get 1% of x
9999999999999999583119736832 # <------------ here

I was expecting 10000000000000000000000000000, how do I achieve this?

dape11
  • 31
  • 4

2 Answers2

2

Floating point arithmetic has Issues and Limitations.

In your case simply avoid it by using integer division:

>>> x=1000000000000000000000000000000
>>> x=x//100
>>> x
10000000000000000000000000000

gimix
  • 3,431
  • 2
  • 5
  • 21
0

You can use decimal to handle the decimal point more precisely.

It provides fast correctly-rounded decimal floating point arithmetic. In contrast, numbers like 1.1 and 2.2 do not have exact representations in binary floating point. Decimal numbers can be represented exactly and in decimal floating point.

from decimal import Decimal
x = Decimal(1000000000000000000000000000000)
print(int(1*x/100))  # 10000000000000000000000000000
Lazyer
  • 917
  • 1
  • 6
  • 16