C
C
copyerfiled2018-01-30 18:51:24
Python
copyerfiled, 2018-01-30 18:51:24

How to discard part of the fraction after the dot?

Good afternoon!
Tell me how in python3 to discard part of the fraction after the point, indicating how many characters should be. By analogy with round, only it does not fit because it rounds up.
For example:
>>> var1 = round(0.0019, 3)
>>> print(var1)
0.002
and you need 0.001
It is not known in advance how many characters there will be, maybe 0.2342354324 or maybe 0.1 should work correctly.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2018-01-30
@copyerfiled

Some fierce Bangalore star marked with a solution (

def truncate(f, n):
    s = "%.20f" % f
    return s[:s.find('.') + n + 1]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question