87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
|
package utility
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"golang.org/x/text/language"
|
||
|
"golang.org/x/text/message"
|
||
|
)
|
||
|
|
||
|
func StringToFloat(num string) float64 {
|
||
|
num = FilterNumberString(num)
|
||
|
floatVal, err := strconv.ParseFloat(num, 64)
|
||
|
|
||
|
if err != nil {
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
return floatVal
|
||
|
}
|
||
|
|
||
|
func ConvertDecimalCommaToDecimalPoint(item string) string {
|
||
|
item = strings.ReplaceAll(item, ",", "-$-")
|
||
|
item = strings.ReplaceAll(item, ".", ",")
|
||
|
item = strings.ReplaceAll(item, "-$-", ".")
|
||
|
|
||
|
return item
|
||
|
}
|
||
|
|
||
|
func FloatToString(input float64) string {
|
||
|
return strconv.FormatFloat(input, 'f', 2, 64)
|
||
|
|
||
|
}
|
||
|
func FloatToStringWithPrecision(input float64, precision int) string {
|
||
|
return strconv.FormatFloat(input, 'f', precision, 64)
|
||
|
}
|
||
|
|
||
|
func FloatToFormattedString(input float64, precision int, t language.Tag) string {
|
||
|
p := message.NewPrinter(t)
|
||
|
return p.Sprintf("%."+strconv.Itoa(precision)+"f\n", input)
|
||
|
}
|
||
|
|
||
|
func SetFloatPrecision(num float64, precision int) float64 {
|
||
|
output := math.Pow(10, float64(precision))
|
||
|
return float64(round(num*output)) / output
|
||
|
}
|
||
|
func round(num float64) int {
|
||
|
return int(num + math.Copysign(0.5, num))
|
||
|
}
|
||
|
|
||
|
func IsNumeric(s string) bool {
|
||
|
s = FilterNumberString(s)
|
||
|
_, err := strconv.ParseFloat(s, 64)
|
||
|
return err == nil
|
||
|
}
|
||
|
|
||
|
func FilterNumberString(number string) string {
|
||
|
number = strings.ReplaceAll(number, ",", "")
|
||
|
return number
|
||
|
}
|
||
|
|
||
|
func DateToYear(t time.Time) int {
|
||
|
year, _, _ := t.Date()
|
||
|
return year
|
||
|
}
|
||
|
|
||
|
func DateToMonth(t time.Time) time.Month {
|
||
|
_, month, _ := t.Date()
|
||
|
return month
|
||
|
}
|
||
|
|
||
|
func GetTimeFromString(timeString string) time.Time {
|
||
|
|
||
|
layoutUS := "02 Jan 2006"
|
||
|
t, err := time.Parse(layoutUS, timeString)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
return time.Time{}
|
||
|
}
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
//
|