Reputation: 22247
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.
Upvotes: 2
Views: 1399
Reputation: 82924
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.
>>>
Upvotes: 6
Reputation: 1799
If you want to compare two floats, you can compare on abs(a-b) < epsilon
where epsilon is your precision requirement.
Upvotes: 3
Reputation: 342333
you can use decimal module, especially the part on getcontext().prec
Upvotes: 1