utils/conversions.go

35 lines
749 B
Go
Raw Normal View History

2023-10-02 23:42:16 +02:00
package utils
import (
"fmt"
"strconv"
)
// ConvertIntToStringAndAddFrontSpacing converts i into a string of a fixed length by adding spacing in the front
func ConvertIntToStringAndAddFrontSpacing(i int64, l int) string {
return fmt.Sprintf(
fmt.Sprintf("%%%dd", l),
i,
)
}
// ConvertStringToFloatOrZeroOnError converts s to float64, or returns 0.0 if this fails
func ConvertStringToFloatOrZeroOnError(s string) float64 {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0.0
}
return f
}
// ConvertStringToIntOrZeroOnError tries to convert s to an int64, or returns 0 if this fails
func ConvertStringToIntOrZeroOnError(s string) int64 {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0
}
return i
}