FormatInteger
This commit is contained in:
parent
3de0d54d91
commit
0238b0512f
39
util.go
39
util.go
|
@ -51,6 +51,45 @@ func FormatBytes(bytes int64) string {
|
||||||
return fmt.Sprintf("%.2f%cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
return fmt.Sprintf("%.2f%cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FormatInteger(num int) string {
|
||||||
|
if num == 0 {
|
||||||
|
return "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
negative := false
|
||||||
|
if num < 0 {
|
||||||
|
negative = true
|
||||||
|
num = -num
|
||||||
|
}
|
||||||
|
|
||||||
|
str := strconv.Itoa(num)
|
||||||
|
var result strings.Builder
|
||||||
|
|
||||||
|
length := len(str)
|
||||||
|
remainder := length % 3
|
||||||
|
if remainder == 0 {
|
||||||
|
remainder = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, char := range str {
|
||||||
|
if i == 0 {
|
||||||
|
result.WriteRune(char)
|
||||||
|
if remainder == 1 && length > 1 {
|
||||||
|
result.WriteString(",")
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (i-remainder+1)%3 == 0 {
|
||||||
|
result.WriteString(",")
|
||||||
|
}
|
||||||
|
result.WriteRune(char)
|
||||||
|
}
|
||||||
|
if negative {
|
||||||
|
return "-" + result.String()
|
||||||
|
}
|
||||||
|
return result.String()
|
||||||
|
}
|
||||||
|
|
||||||
func If[T any](condition bool, trueVal, falseVal T) T {
|
func If[T any](condition bool, trueVal, falseVal T) T {
|
||||||
if condition {
|
if condition {
|
||||||
return trueVal
|
return trueVal
|
||||||
|
|
Loading…
Reference in New Issue