I am using the following to print out timestamps:
strftime('%d-%m-%Y %H:%M:%S.%f')
However I want to round the microseconds to two decimal places rather than print out to six decimal places. Is there an easier way to achieve this rather than 'unpacking' all the time elements, formatting and rounding the microseconds to 2 decimals, and formatting a new 'print string'?
Based on jfs' answer, I added one more statement as
replace(microsecond=round(d.microsecond, ndigits))
may give an error : ValueError: microsecond must be in 0..999999.
i.e. if microsecond is from 995000 to 999999 round(microsecond, ndigits) will give 1000000.
You'll have to round yourself; use string formatting to format the date without microseconds, then add in the top two digits of the
microsecond
attribute separately:Demo: