0

I have a pandas dataframe, with columns [QuantityReq] as Float and [Test] as Float. I am trying to get 3 digits after the decimal points. The [Test] column which I create is truncating for 3 digits after decimal, but in some cases it is rounding at the 3rd digit. I have highlighted the error in row 2 and 3 of [Test] column where it is showing 0.869 instead of 0.87 and 59.306 instead of 59.307

enter image description here

Any help is greatly appreciated.

df['Test'] =  np.trunc( df['QuantityReq']* 1000)/1000
Chris
  • 26,361
  • 5
  • 21
  • 42

1 Answers1

0

Try using decimal module it's preferred to deal with float number in python as float numbers have some problems that mention here.

sample example

from decimal import Decimal

for i in [59.307000, 0.8700000]:
    print(Decimal(i).quantize(Decimal("1.000")))

output

59.307
0.870

for more check this.