hetica
hetica

Reputation: 53

Decrease precision of float and int

From a large set of data, I would like to decrease the precision of each number, float or int (Python3) :

12345678 --> 12340000
1.2345678 --> 1.234
0.12345678 --> 0.1234
1.2345678e-05 --> 1.234e-05

But how to do this?

Upvotes: 0

Views: 57

Answers (1)

Lutz
Lutz

Reputation: 655

one way would be to use log10 to detect the highest digit:

a=[12345678, 1.2345678, 0.12345678, 1.2345678e-05]
b=[math.pow(10,int(-math.log10(x))+4) for x in a]
c=[int(x*y)/y for x,y in zip(a,b)]

c is then equal [12345000.0, 1.2345, 0.12345, 1.2345e-05]

Upvotes: 1

Related Questions