@@ -243,16 +243,13 @@ def weighted_sampler(seq, weights):
243243
244244 return lambda : seq [bisect .bisect (totals , random .uniform (0 , totals [- 1 ]))]
245245
246- def truncate (x , n = 4 ):
247- """Truncates floats, vectors, matrices to n decimal values"""
248- if isinstance (x , float ):
249- return (float ("{0:.{1}f}" .format (x , n )))
250- elif isinstance (x , list ) and isinstance (x [0 ], float ):
251- return ([float ("{0:.{1}f}" .format (i , n )) for i in x ])
252- elif isinstance (x , list ) and isinstance (x [0 ], list ) and isinstance (x [0 ][0 ], float ):
253- return ([[float ("{0:.{1}f}" .format (i , n )) for i in row ] for row in x ])
246+ def rounder (numbers , d = 4 ):
247+ "Round a single number, or sequence of numbers, to d decimal places."
248+ if isinstance (numbers , (int , float )):
249+ return round (numbers , d )
254250 else :
255- return x
251+ constructor = type (numbers ) # Can be list, set, tuple, etc.
252+ return constructor (rounder (n , d ) for n in numbers )
256253
257254def num_or_str (x ):
258255 """The argument is a string; convert to a number if
0 commit comments