index.go 3.65 KB
package lxconv

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
)

// IntToStr 数字转换字符串
func IntToStr(i int) string {
	return strconv.Itoa(i)
}

// Int64ToStr 64位数字转换字符串
func Int64ToStr(i int64) string {
	return strconv.FormatInt(i, 10)
}

// StrToInt 字符串转换位int
func StrToInt(str string) int {
	intnum, _ := strconv.Atoi(str)
	return intnum
}

// StrToInt64 字符串转换位int64
func StrToInt64(str string) int64 {
	intnum, _ := strconv.ParseInt(str, 10, 64)
	return intnum
}

// StrToFloat64 字符串转换浮点数
func StrToFloat64(str string, i int) float64 {
	v2, _ := strconv.ParseFloat(str, i)
	return v2
}

// ParseInt64 字符串转换位int64
func ParseInt64(val interface{}) int64 {

	if val == nil {
		return 0
	}

	switch ret := val.(type) {
	case string:
		return StrToInt64(ret)
	case int8:
		return int64(ret)
	case uint8:
		return int64(ret)
	case int16:
		return int64(ret)
	case uint16:
		return int64(ret)
	case int:
		return int64(ret)
	case uint:
		return int64(ret)
	case int64:
		return int64(ret)
	case uint64:
		return int64(ret)
	case float32:
		return int64(ret)
	case float64:
		return int64(ret)
	}

	return 0
}

// ParseInt 字符串转换位int
func ParseInt(val interface{}) int {

	if val == nil {
		return 0
	}

	switch ret := val.(type) {
	case string:
		return StrToInt(ret)
	case int8:
		return int(ret)
	case uint8:
		return int(ret)
	case int16:
		return int(ret)
	case uint16:
		return int(ret)
	case int:
		return int(ret)
	case uint:
		return int(ret)
	case int64:
		return int(ret)
	case uint64:
		return int(ret)
	case float32:
		return int(ret)
	case float64:
		return int(ret)
	}

	return 0
}

// FloatTostr 浮点数转换int卡类型
func FloatTostr(floatstr float64, i int) string {
	return strconv.FormatFloat(floatstr, 'E', -1, i)
}

// JsonEncode json转字符串
func JsonEncode(data interface{}) string {
	datastr, _ := json.Marshal(data)
	return string(datastr)
}

// ToInt 任意类型强转int
func ToInt(a interface{}) int {

	ai := 0

	switch a := a.(type) {
	case float64:
		ai = int(a)
	case float32:
		ai = int(a)
	case int:
		ai = a
	case int32:
		ai = int(a)
	case int64:
		ai = int(a)
	case string:
		ai = StrToInt(a)

	default:

	}

	return ai
}

// ToInt64 任意类型强转int64
func ToInt64(a interface{}) int64 {

	var ai int64

	switch a := a.(type) {
	case float64:
		ai = int64(a)
	case float32:
		ai = int64(a)
	case int:
		ai = int64(a)
	case int32:
		ai = int64(a)
	case int64:
		ai = a
	case string:
		ai = StrToInt64(a)

	default:

	}

	return ai
}

// ToFloat64 任意类型强转float64
func ToFloat64(a interface{}) float64 {

	var ai float64

	switch a := a.(type) {
	case float64:
		ai = a
	case float32:
		ai = float64(a)
	case int:
		ai = float64(a)
	case int32:
		ai = float64(a)
	case int64:
		ai = float64(a)
	case string:
		ai = StrToFloat64(a, 64)

	default:

	}

	return ai
}

// ToString 任意类型强转字符串
func ToString(a interface{}) string {

	var ai string

	switch a := a.(type) {
	case float64:
		ai = strconv.FormatFloat(a, 'E', -1, 64)
	case float32:
		ai = strconv.FormatFloat(float64(a), 'E', -1, 64)
	case int:
		ai = fmt.Sprintf("%d", a)
	case int32:
		ai = fmt.Sprintf("%d", a)
	case int64:
		ai = fmt.Sprintf("%d", a)
	case string:
		ai = fmt.Sprintf("%s", a)
	default:
		ai = fmt.Sprintf("%v", a)

	}

	return ai
}

func InterfaceToStr(v interface{}) string {
	str := ""
	switch v.(type) {
	case string:
		str = v.(string)
	default:
		jsonbyte, _ := json.Marshal(v)
		str = string(jsonbyte)
	}
	return str
}

// ToBool 转换 bool类型
func ToBool(a interface{}) bool {

	s := ToString(a)
	if s == "1" || strings.ToLower(s) == "true" {
		return true
	}

	return false

}