Codebase list golang-github-vbauerster-mpb / 690a3da internal / percentage.go
690a3da

Tree @690a3da (Download .tar.gz)

percentage.go @690a3daraw · history · blame

package internal

import "math"

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

func PercentageRound(total, current int64, width int) float64 {
	return math.Round(Percentage(total, current, width))
}

func CalcWidthForBarFiller(reqWidth, available int) int {
	if reqWidth <= 0 || reqWidth >= available {
		return available
	}
	return reqWidth
}