views:

65

answers:

4

Using numpy or python's standard library, either or. How can I take a value with several decimal places and truncate it to 4 decimal places? I only want to compare floating point numbers to their first 4 decimal points.

+1  A: 

you can use decimal module, especially the part on getcontext().prec

ghostdog74
+3  A: 

If you want to compare two floats, you can compare on abs(a-b) < epsilon where epsilon is your precision requirement.

Goran Rakic
+6  A: 

round(a_float, 4)

>>> help(round)
Help on built-in function round in module __builtin__:

round(...)
    round(number[, ndigits]) -> floating point number

    Round a number to a given precision in decimal digits (default 0 digits).
    This always returns a floating point number.  Precision may be negative.

>>>
John Machin
+2  A: 
>>> round(1.2345678,4) == round(1.2345999,4)
True
gnibbler