File: percentage.go

package info (click to toggle)
golang-github-vbauerster-mpb 8.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,044 kB
  • sloc: sh: 7; makefile: 3
file content (22 lines) | stat: -rw-r--r-- 517 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package internal

import "math"

// Percentage is a helper function, to calculate percentage.
func Percentage(total, current, width uint) float64 {
	if total == 0 {
		return 0
	}
	if current >= total {
		return float64(width)
	}
	return float64(width*current) / float64(total)
}

// PercentageRound same as Percentage but with math.Round.
func PercentageRound(total, current int64, width uint) float64 {
	if total < 0 || current < 0 {
		return 0
	}
	return math.Round(Percentage(uint(total), uint(current), width))
}