From a1748cc324a27a56571f640925da0147a5f150f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 8 May 2023 17:15:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96float=E5=8C=85=E8=A3=85?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- float.go | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/float.go b/float.go index 3f4fa39..7179322 100644 --- a/float.go +++ b/float.go @@ -24,11 +24,17 @@ type Float float64 // 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", float32(math.SmallestNonzeroFloat32), float32(math.MaxFloat32)) +func (f Float) ToFloat32() Float32Result { + res := Float32Result{ + Value: 0, + Err: nil, } - return float32(f), nil + if f > math.MaxFloat32 || f < math.SmallestNonzeroFloat32 { + res.Err = fmt.Errorf("float32 should between %v and %v", float32(math.SmallestNonzeroFloat32), float32(math.MaxFloat32)) + return res + } + res.Value = float32(f) + return res } // ToFloat64 ... @@ -36,11 +42,17 @@ func (f Float) ToFloat32() (float32, error) { // 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", float64(math.SmallestNonzeroFloat64), float64(math.MaxFloat64)) +func (f Float) ToFloat64() Float64Result { + res := Float64Result{ + Value: 0, + Err: nil, } - return float64(f), nil + if f > math.MaxFloat64 || f < math.SmallestNonzeroFloat64 { + res.Err = fmt.Errorf("float32 should between %v and %v", float64(math.SmallestNonzeroFloat64), float64(math.MaxFloat64)) + return res + } + res.Value = float64(f) + return res } // ToString ... @@ -48,10 +60,16 @@ func (f Float) ToFloat64() (float64, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 14:38 2023/5/5 -func (f Float) ToString() (string, error) { - floatVal, err := f.ToFloat64() - if nil != err { - return "", err +func (f Float) ToString() StringResult { + floatVal := f.ToFloat64() + if nil != floatVal.Err { + return StringResult{ + Value: "", + Err: floatVal.Err, + } + } + return StringResult{ + Value: fmt.Sprintf("%v", floatVal), + Err: nil, } - return fmt.Sprintf("%v", floatVal), nil }