25

I want to truncate the float values within the numpy array, for .e.g.

2.34341232 --> 2.34 

I read the post truncate floating point but its for one float. I don't want to run a loop on the numpy array, it will be quite expensive. Is there any inbuilt method within numpy which can do this easily? I do need output as a float not string.

Community
  • 1
  • 1
blackbug
  • 1,098
  • 3
  • 13
  • 40
  • 1
    Numpy provides the **around** method. The syntax is `np.around(numpy_array, num_decimals)`. For example: `a = np.array([2.3434]), np.around(a, 2) --> produces array([2.34])`. Is this what are you looking for? – Cristian Ramon-Cortes Feb 03 '17 at 10:28
  • this looks like a dupe: http://stackoverflow.com/questions/21008858/formatting-floats-in-a-numpy-array/21009774#21009774 – EdChum Feb 03 '17 at 10:31
  • 1
    Do you actually want to discard the data after the 2nd decimal place, or do you just want to change how the data is displayed? If the former, use the `numpy.around` or `numpy.round` method. – PM 2Ring Feb 03 '17 at 10:37
  • @AlonAlexander `num = ((num*100)//1)/100` this logic only works for positive values. For example to truncate negative value to one decimal , `x = -2.134` then `(-2.134*10//1)10` gives `-3`, which is not an acceptable answer.! – Girish Kumar Chandora Apr 13 '21 at 09:50

2 Answers2

38

Try out this modified version of numpy.trunc().

import numpy as np
def trunc(values, decs=0):
    return np.trunc(values*10**decs)/(10**decs)

Sadly, numpy.trunc function doesn't allow decimal truncation. Luckily, multiplying the argument and dividing it's result by a power of ten give the expected results.

vec = np.array([-4.79, -0.38, -0.001, 0.011, 0.4444, 2.34341232, 6.999])

trunc(vec, decs=2)

which returns:

>>> array([-4.79, -0.38, -0.  ,  0.01,  0.44,  2.34,  6.99])
Traxidus Wolf
  • 808
  • 1
  • 9
  • 18
  • 4
    Happy to report this seems to run in only about half the time as `np.around` for anyone switching their rounding -> truncating logic. – Milo Wielondek Oct 30 '20 at 13:40
16

Use numpy.round:

import numpy as np
a = np.arange(4) ** np.pi
a
=> array([  0.        ,   1.        ,   8.82497783,  31.5442807 ])
a.round(decimals=2)
=> array([  0.  ,   1.  ,   8.82,  31.54])
shx2
  • 61,779
  • 13
  • 130
  • 153
  • 49
    How is this the answer when the question is for *truncation*, not rounding? (I know, I know, the asker accepted it. So I'm really mystified at them, even more so than this answer.) – John Y May 25 '17 at 19:57
  • 4
    Rounding might be subtle when not carefull. Please see my answer for real truncation. – Traxidus Wolf May 27 '19 at 06:45
  • 4
    I think this answer is not correct to the question. Casually, the third decimal is less than 5 in all array elements and for this reason the output is successful, however if you change the decimal numbers ( 'a.round(decimals=3)') to 1 or 3, it outputs 8.825, what is wrong, and if you want to truncate, it should output 8.254 – Julio CamPlaz Nov 17 '20 at 09:02