Skip to content

Commit

Permalink
计算平均数时,如果总数为0,则直接返回0
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Aug 31, 2024
1 parent e02830f commit e82d341
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion enumerable.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,11 @@ func (receiver Enumerable[T]) Average(fn func(item T) any) float64 {
receiver.lock.RLock()
defer receiver.lock.RUnlock()

sum := receiver.Sum(fn)
count := len(*receiver.source)
if count ==0 {
return 0
}
sum := receiver.Sum(fn)
return parse.Convert(sum, float64(0)) / parse.Convert(count, float64(0))
}

Expand Down

0 comments on commit e82d341

Please sign in to comment.