浏览代码

no divide by zero

Michael Yang 1 年之前
父节点
当前提交
4d677ee389
共有 1 个文件被更改,包括 6 次插入1 次删除
  1. 6 1
      progress/bar.go

+ 6 - 1
progress/bar.go

@@ -105,7 +105,12 @@ func (b *Bar) percent() float64 {
 }
 }
 
 
 func (b *Bar) rate() float64 {
 func (b *Bar) rate() float64 {
-	return (float64(b.currentValue) - float64(b.initialValue)) / b.elapsed().Seconds()
+	elapsed := b.elapsed()
+	if elapsed.Seconds() > 0 {
+		return (float64(b.currentValue) - float64(b.initialValue)) / elapsed.Seconds()
+	}
+
+	return 0
 }
 }
 
 
 func (b *Bar) elapsed() time.Duration {
 func (b *Bar) elapsed() time.Duration {