diff --git a/float.go b/float.go new file mode 100644 index 0000000..1fa853d --- /dev/null +++ b/float.go @@ -0,0 +1,53 @@ +// Package wrapper ... +// +// Description : wrapper ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-05 14:33 +package wrapper + +import ( + "fmt" + "math" +) + +// Float ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:33 2023/5/5 +type Float float64 + +// ToFloat32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:36 2023/5/5 +func (f Float) ToFloat32() (float32, error) { + if f > math.MaxFloat32 || f < math.SmallestNonzeroFloat32 { + return 0, fmt.Errorf("float32 should between %v and %v", math.SmallestNonzeroFloat32, math.MaxFloat32) + } + return float32(f), nil +} + +// ToFloat64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:36 2023/5/5 +func (f Float) ToFloat64() (float64, error) { + if f > math.MaxFloat64 || f < math.SmallestNonzeroFloat64 { + return 0, fmt.Errorf("float32 should between %v and %v", math.SmallestNonzeroFloat64, math.MaxFloat64) + } + return float64(f), nil +} + +// ToString ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:38 2023/5/5 +func (f Float) ToString() string { + return fmt.Sprintf("%v", f) +}