Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python setting Decimal Place range without rounding?

You can do:

def truncate(f, n):
    return math.floor(f * 10 ** n) / 10 ** n

testing:

>>> f=1.923328437452
>>> [truncate(f, n) for n in range(7)]
[1.0, 1.9, 1.92, 1.923, 1.9233, 1.92332, 1.923328]

A super simple solution is to use strings

x = float (str (w)[:-1])
y = float (str (w)[:-2])
z = float (str (w)[:-3])

Any of the floating point library solutions would require you dodge some rounding, and using floor/powers of 10 to pick out the decimals can get a little hairy by comparison to the above.


Integers are faster to manipulate than floats/doubles which are faster than strings. In this case, I tried to get time with both approach :

  timeit.timeit(stmt = "float(str(math.pi)[:12])", setup = "import math", number = 1000000)

~1.1929605630000424

for :

timeit.timeit(stmt = "math.floor(math.pi * 10 ** 10) / 10 ** 10", setup = "import math", number = 1000000)

~0.3455968870000561

So it's safe to use math.floor rather than string operation on it.


If you just need to control the precision in format

pi = 3.14159265
format(pi, '.3f') #print 3.142 # 3 precision after the decimal point
format(pi, '.1f') #print 3.1
format(pi, '.10f') #print 3.1415926500, more precision than the original

If you need to control the precision in floating point arithmetic

import decimal
decimal.getcontext().prec=4 #4 precision in total
pi = decimal.Decimal(3.14159265)
pi**2 #print Decimal('9.870') whereas '3.142 squared' would be off

--edit--

Without "rounding", thus truncating the number

import decimal
from decimal import ROUND_DOWN
decimal.getcontext().prec=4
pi*1 #print Decimal('3.142')

decimal.getcontext().rounding = ROUND_DOWN
pi*1 #print Decimal('3.141')