मैं तैरने के परिणामों (बाहरी से) के साथ काम कर रहा हूंपायल्स स्रोत) पायथन में और मुझे एक फ्लोट को समय प्रारूप में बदलना होगा - मिनट, सेकंड और माइक्रोसेकंड - संचालन जोड़ने और घटाने के लिए। मैं इस फ़ंक्शन का उपयोग कर रहा हूं:
from datetime import timedelta
def format_result(result):
seconds = int(result)
microseconds = int((result * 1000000) % 1000000)
output = timedelta(0, seconds, microseconds)
return output
जब दिया गया इनपुट 131.39 है, तो आउटपुट 0: 02: 11.390000 होना चाहिए लेकिन वास्तव में 0: 02: 11.389999 है। मैं इस सटीक त्रुटि के बिना इसे सही तरीके से कैसे बदल सकता हूं?
उत्तर:
जवाब के लिए 0 № 1मैंने इसे बिना रूपांतरित किए प्रयास किया है int
, यह ठीक काम करता है, के रूप में अपने प्रदर्शन यह 390000 देता है .... !!
from datetime import timedelta
def format_result(result):
seconds = int(result)
microseconds = (result * 1000000) % 1000000
output = timedelta(0, seconds, microseconds)
return output
print format_result(131.39)
जवाब के लिए 2 № 2
बस आपको इसे UTC समय में बदलना है और format
दिनांक समय
>>> import datetime
>>> datetime.datetime.strftime(datetime.datetime.utcfromtimestamp(131.39), "%M:%S:%f")
"02:11:390000"
आपको क्या करने की आवश्यकता है,
import datetime
def format_result(result):
date = datetime.datetime.utcfromtimestamp(result)
output = datetime.datetime.strftime(date, "%M:%S:%f")
return output
print format_result(131.39)
उंमीद है कि यह मदद मिलेगी ।