From fa2f27724d766da17bd7090f8623f186585de3ec 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 16:07:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Derror=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- float.go | 12 ++++++++---- int.go | 18 +++++++++++------- uint.go | 18 +++++++++++------- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/float.go b/float.go index 1fa853d..3f4fa39 100644 --- a/float.go +++ b/float.go @@ -26,7 +26,7 @@ type Float float64 // 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 0, fmt.Errorf("float32 should between %v and %v", float32(math.SmallestNonzeroFloat32), float32(math.MaxFloat32)) } return float32(f), nil } @@ -38,7 +38,7 @@ func (f Float) ToFloat32() (float32, error) { // 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 0, fmt.Errorf("float32 should between %v and %v", float64(math.SmallestNonzeroFloat64), float64(math.MaxFloat64)) } return float64(f), nil } @@ -48,6 +48,10 @@ func (f Float) ToFloat64() (float64, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 14:38 2023/5/5 -func (f Float) ToString() string { - return fmt.Sprintf("%v", f) +func (f Float) ToString() (string, error) { + floatVal, err := f.ToFloat64() + if nil != err { + return "", err + } + return fmt.Sprintf("%v", floatVal), nil } diff --git a/int.go b/int.go index bb4b32c..d581cc4 100644 --- a/int.go +++ b/int.go @@ -26,7 +26,7 @@ type Int int64 // Date : 13:57 2023/5/5 func (i Int) ToInt8() (int8, error) { if math.MaxInt8 < int64(i) || math.MinInt8 > int64(i) { - return 0, fmt.Errorf("int8 value should between %v and %v ", math.MinInt8, math.MaxInt8) + return 0, fmt.Errorf("int8 value should between %v and %v ", int8(math.MinInt8), int8(math.MaxInt8)) } return int8(i), nil } @@ -38,7 +38,7 @@ func (i Int) ToInt8() (int8, error) { // Date : 14:05 2023/5/5 func (i Int) ToInt16() (int16, error) { if math.MaxInt16 < int64(i) || math.MinInt16 > int64(i) { - return 0, fmt.Errorf("int16 value should between %v and %v ", math.MinInt16, math.MaxInt16) + return 0, fmt.Errorf("int16 value should between %v and %v ", int16(math.MinInt16), int16(math.MaxInt16)) } return int16(i), nil } @@ -50,7 +50,7 @@ func (i Int) ToInt16() (int16, error) { // Date : 14:05 2023/5/5 func (i Int) ToInt32() (int32, error) { if math.MaxInt32 < int64(i) || math.MinInt32 > int64(i) { - return 0, fmt.Errorf("int32 value should between %v and %v ", math.MinInt32, math.MaxInt32) + return 0, fmt.Errorf("int32 value should between %v and %v ", int32(math.MinInt32), int32(math.MaxInt32)) } return int32(i), nil } @@ -62,7 +62,7 @@ func (i Int) ToInt32() (int32, error) { // Date : 14:06 2023/5/5 func (i Int) ToInt64() (int64, error) { if math.MaxInt64 < i || math.MinInt64 > i { - return 0, fmt.Errorf("int64 value should between %v and %v ", math.MinInt64, math.MaxInt64) + return 0, fmt.Errorf("int64 value should between %v and %v ", int64(math.MinInt64), int64(math.MaxInt64)) } return int64(i), nil } @@ -74,7 +74,7 @@ func (i Int) ToInt64() (int64, error) { // Date : 14:07 2023/5/5 func (i Int) ToInt() (int, error) { if math.MaxInt < i || math.MinInt > i { - return 0, fmt.Errorf("int value should between %v and %v ", math.MinInt, math.MaxInt) + return 0, fmt.Errorf("int value should between %v and %v ", int(math.MinInt), int(math.MaxInt)) } return int(i), nil } @@ -84,6 +84,10 @@ func (i Int) ToInt() (int, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 14:07 2023/5/5 -func (i Int) ToString() string { - return fmt.Sprintf("%v", i) +func (i Int) ToString() (string, error) { + in64Val, err := i.ToInt64() + if nil != err { + return "", err + } + return fmt.Sprintf("%v", in64Val), nil } diff --git a/uint.go b/uint.go index c48be8d..0aace1f 100644 --- a/uint.go +++ b/uint.go @@ -26,7 +26,7 @@ type Uint uint64 // Date : 14:21 2023/5/5 func (ui Uint) ToUint8() (uint8, error) { if ui > math.MaxUint8 || ui < 0 { - return 0, fmt.Errorf("uint8 should between 0 and %v", math.MaxUint8) + return 0, fmt.Errorf("uint8 should between 0 and %v", uint8(math.MaxUint8)) } return uint8(ui), nil } @@ -38,7 +38,7 @@ func (ui Uint) ToUint8() (uint8, error) { // Date : 14:25 2023/5/5 func (ui Uint) ToUint16() (uint16, error) { if ui > math.MaxUint16 || ui < 0 { - return 0, fmt.Errorf("uint16 should between 0 and %v", math.MaxUint16) + return 0, fmt.Errorf("uint16 should between 0 and %v", uint16(math.MaxUint16)) } return uint16(ui), nil } @@ -50,7 +50,7 @@ func (ui Uint) ToUint16() (uint16, error) { // Date : 14:25 2023/5/5 func (ui Uint) ToUint32() (uint32, error) { if ui > math.MaxUint32 || ui < 0 { - return 0, fmt.Errorf("uint32 should between 0 and %v", math.MaxUint32) + return 0, fmt.Errorf("uint32 should between 0 and %v", uint32(math.MaxUint32)) } return uint32(ui), nil } @@ -62,7 +62,7 @@ func (ui Uint) ToUint32() (uint32, error) { // Date : 14:30 2023/5/5 func (ui Uint) ToUint64() (uint64, error) { if ui > math.MaxUint64 || ui < 0 { - return 0, fmt.Errorf("uint64 should between 0 and %v", math.MaxUint64) + return 0, fmt.Errorf("uint64 should between 0 and %v", uint64(math.MaxUint64)) } return uint64(ui), nil } @@ -74,7 +74,7 @@ func (ui Uint) ToUint64() (uint64, error) { // Date : 14:31 2023/5/5 func (ui Uint) ToUint() (uint, error) { if ui > math.MaxUint || ui < 0 { - return 0, fmt.Errorf("uint should between 0 and %v", math.MaxUint) + return 0, fmt.Errorf("uint should between 0 and %v", uint(math.MaxUint)) } return uint(ui), nil } @@ -84,6 +84,10 @@ func (ui Uint) ToUint() (uint, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 14:32 2023/5/5 -func (ui Uint) ToString() string { - return fmt.Sprintf("%v", ui) +func (ui Uint) ToString() (string, error) { + uint64Val, err := ui.ToUint64() + if nil != err { + return "", err + } + return fmt.Sprintf("%v", uint64Val), nil }