从整数中解析小数

时间:2016-12-27 01:36:26

标签: python python-3.x parsing tuples

我的代码/项目:我正在学习Python并试图为一些马拉松训练创建一个步速计算器。

我的问题:如何从计算的最终数字中分离/解析小数?

示例:使用以下输入值recordMinutes = int(input("What is your total minutes? ")) for steps in range(1): converted = recordMinutes * 60 recordSeconds = int(input("What is your total seconds leftover? ")) for steps in range(1): combined = converted + recordSeconds distance = float(input("What is the distance? ")) paceRaw = combined/distance paceBasic = (paceRaw/60) #This is where I would multiply the ".68" by 60 pace = (#decimal remainder * 60) + #whole number print ("You need to run %d per mile " % pace)

整个代码会给我一个340.96秒的答案。如果我将它除以60以找出确切的分钟/秒;答案变为5.68。

我需要取“.68”并将其乘以60以获得实际秒数,因为它代表68%的一分钟。

<!DOCTYPE html>
<html>
<body>

<h3>Example Code</h3>
<button class="button" onclick="Addbanana()">Add 1 Banana</button>
  <p>Bananas = <span id="banana"> 0 </span></p>
  <p>Bananas = <span id="banana"> 0 </span></p>
  <p>Bananas = <span id="banana"> 0 </span></p>


<script>
var banana = 0;
function Addbanana(){
    banana = banana + 1;
    document.getElementById("banana").innerHTML = banana;
}
</script>

</body>
</html> 

2 个答案:

答案 0 :(得分:3)

使用divmod

>>> divmod(340.96, 60)
(5.0, 40.95999999999998)

答案 1 :(得分:0)

作为初学者的一个好习惯是在没有任何库或方法的情况下执行此操作:

>>> s = 5.68
>>> s = s*100
568.0
>>> s = s%100
68.0
>>> s/100
0.68