Compare commits
3 Commits
ad6b8211b3
...
372fd452c7
| Author | SHA1 | Date | |
|---|---|---|---|
| 372fd452c7 | |||
| e28948f8cd | |||
| 9ed193cc48 |
@@ -0,0 +1,50 @@
|
|||||||
|
package proxmoxaas_common_lib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const Base1000 uint64 = 1000
|
||||||
|
const Base1024 uint64 = 1024
|
||||||
|
|
||||||
|
var prefixesBase1000 []string = []string{"", "K", "M", "G", "T"}
|
||||||
|
var prefixesBase1024 []string = []string{"", "Ki", "Mi", "Gi", "Ti"}
|
||||||
|
|
||||||
|
const KB uint64 = Base1000
|
||||||
|
const MB uint64 = KB * Base1000
|
||||||
|
const GB uint64 = MB * Base1000
|
||||||
|
const TB uint64 = GB * Base1000
|
||||||
|
|
||||||
|
const KiB uint64 = Base1024
|
||||||
|
const MiB uint64 = KiB * Base1024
|
||||||
|
const GiB uint64 = MiB * Base1024
|
||||||
|
const TiB uint64 = GiB * Base1024
|
||||||
|
|
||||||
|
func FormatNumber(val uint64, base uint64) (string, string) {
|
||||||
|
valf := float64(val)
|
||||||
|
basef := float64(base)
|
||||||
|
steps := 0
|
||||||
|
for math.Abs(valf) > basef && steps < 4 {
|
||||||
|
valf /= basef
|
||||||
|
steps++
|
||||||
|
}
|
||||||
|
|
||||||
|
switch base {
|
||||||
|
case Base1000:
|
||||||
|
s := fmt.Sprintf("%.4f", valf)
|
||||||
|
s = strings.TrimRight(s, "0")
|
||||||
|
s = strings.TrimRight(s, ".")
|
||||||
|
prefixes := prefixesBase1000
|
||||||
|
return s, prefixes[steps]
|
||||||
|
case Base1024:
|
||||||
|
s := fmt.Sprintf("%.4f", valf)
|
||||||
|
s = strings.TrimRight(s, "0")
|
||||||
|
s = strings.TrimRight(s, ".")
|
||||||
|
prefixes := prefixesBase1024
|
||||||
|
return s, prefixes[steps]
|
||||||
|
default:
|
||||||
|
return "0", ""
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user