只是寻找问题的答案,再多的谷歌搜索似乎也无法解决。
如果..
a = 1.23
我希望能够取 1 并乘以这个数字,但保留 0.23
这怎么可能?
提前致谢!
最佳答案
在对 munkhd 的回答的评论中,您说:
I have want to be able to input a value as hours then convert them to minutes. So if it was 1.20 I would multiply the 1 by 60 then add 20. Im sure there must be an easier method :)
因此,您的程序将收到 1.20 作为字符串。所以你可以在上面使用字符串方法,例如
>>> dotted_time = '1.20'
>>> h, m = [int(s) for s in dotted_time.split('.')]
>>> print h, m
1 20
>>> minutes = 60*h + m
>>> hours = minutes / 60.0
>>> print minutes, hours
80 1.33333333333
或者,您可以执行 colon_time = dotted_time.replace('.', ':')
; colon_time
采用标准时间函数可以理解和操作的格式。这可能是更明智的继续方式,如果您想处理像 '1.20.30' 这样的秒时间,.replace('.', ':')
将转换它,那么它会很容易处理到“1:20:30”。
关于python - 如何在 Python 中索引浮点值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919129/