CalculatePercentage safely calculates percentage from used and total values Returns 0.0 if total is 0 to avoid division by zero.
(used, total float64)
| 149 | // CalculatePercentage safely calculates percentage from used and total values |
| 150 | // Returns 0.0 if total is 0 to avoid division by zero. |
| 151 | func CalculatePercentage(used, total float64) float64 { |
| 152 | if total <= 0 { |
| 153 | return 0.0 |
| 154 | } |
| 155 | |
| 156 | return (used / total) * 100 |
| 157 | } |
| 158 | |
| 159 | // CalculatePercentageInt safely calculates percentage from used and total int64 values |
| 160 | // Returns 0.0 if total is 0 to avoid division by zero. |