You can do:
def truncate(f, n):
return math.floor(f * 10 ** n) / 10 ** n
testing:
>>> f=1.923328437452
>>> [truncate(f, n) for n in range(7)]
[1.0, 1.9, 1.92, 1.923, 1.9233, 1.92332, 1.923328]
More Related Contents:
- a python library that accepts some text, and replaces phone numbers, names, and so on with tokens
- urllib and “SSL: CERTIFICATE_VERIFY_FAILED” Error
- Python Variable Declaration
- Python script to do something at the same time every day [duplicate]
- Integer division in Python 2 and Python 3
- Running an Excel macro via Python?
- Datetime Timezone conversion using pytz
- Using openCV to overlay transparent image onto another image
- importing izip from itertools module gives NameError in Python 3.x
- rounding errors in Python floor division
- Where should I put my own python module so that it can be imported
- Decompile Python 2.7 .pyc [closed]
- How to print double quotes around a variable?
- How to set justification on Tkinter Text box
- ‘^’ is ignored by Python – how to escape ‘^’ character in Popen Windows?
- Why is the time complexity of Python’s list.append() method O(1)?
- How to convert integer into date object python?
- Trying to use open(filename, ‘w’ ) gives IOError: [Errno 2] No such file or directory if directory doesn’t exist
- Import Python Script Into Another?
- Can json.loads ignore trailing commas?
- Faithfully Preserve Comments in Parsed XML
- Python: Splat/unpack operator * in python cannot be used in an expression?
- ‘virtualenv’ is not recognized as an internal or external command, operable program or batch file
- Sound generation / synthesis with python?
- Sending a file over TCP sockets in Python
- Calling Python script from C++ and using its output
- Redo for loop iteration in Python
- getattr and setattr on nested subobjects / chained properties?
- pydev breakpoints not working
- Send method using generator. still trying to understand the send method and quirky behaviour