From 082610fcca35ed56e14d19ba80c8f08e116d8c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 5 May 2023 14:39:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=85=E8=A3=85float?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- float.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 float.go 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) +}