Category : decimal

I read that for precise in python I should use Decimal module, but when testing simple calculations I get values that is not that precise. Using python 3.7.12 >>> from decimal import Decimal as dc >>> dc(1.1)*dc(3) Decimal(‘3.300000000000000266453525910’) The result should be 3.3 Solution: >>> dc(‘1.1’)*dc(3) Decimal(‘3.3’) or >>> a = 1.1 >>> dc(str(a))*dc(3) Decimal(‘3.3’) ..

Read more

I have a pandas dataframe like this: df = pd.DataFrame([ {‘A’: ‘aaa’, ‘B’: 0.01, ‘C’: 0.00001, ‘D’: 0.00999999999476131, ‘E’: 0.00023191546403037534}, {‘A’: ‘bbb’, ‘B’: 0.01, ‘C’: 0.0001, ‘D’: 0.010000000000218279, ‘E’: 0.002981781316158273}, {‘A’: ‘ccc’, ‘B’: 0.1, ‘C’: 0.001, ‘D’: 0.0999999999999659, ‘E’: 0.020048115477145148}, {‘A’: ‘ddd’, ‘B’: 0.01, ‘C’: 0.01, ‘D’: 0.019999999999999574, ‘E’: 0.397456279809221}, {‘A’: ‘eee’, ‘B’: 0.00001, ‘C’: ..

Read more

In a django project I’m trying to store a calculated decimal value, but it fails for some strange reason. My model looks something like: class FancyModel(models.Model): fancy_field = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True) Sometimes, rather often actually, it crashes on save: django.db.utils.IntegrityError: CHECK constraint failed: myapp_fancy_model which I found out after some digging was caused by ..

Read more

I have this function def dec(x): """Convert to Decimal and remove exponent and trailing zeros""" if not x: return Decimal(0) if not isinstance(x, Decimal): x = Decimal(str(x)) return x.quantize(Decimal(1)) if x == x.to_integral() else x.normalize() and in pandas I would do df[‘price’].apply(dec). However, dask doesn’t support this so what is another way to convert a ..

Read more