2023-05-05 14:39:06 +08:00
|
|
|
// 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 {
|
2023-05-05 16:07:42 +08:00
|
|
|
return 0, fmt.Errorf("float32 should between %v and %v", float32(math.SmallestNonzeroFloat32), float32(math.MaxFloat32))
|
2023-05-05 14:39:06 +08:00
|
|
|
}
|
|
|
|
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 {
|
2023-05-05 16:07:42 +08:00
|
|
|
return 0, fmt.Errorf("float32 should between %v and %v", float64(math.SmallestNonzeroFloat64), float64(math.MaxFloat64))
|
2023-05-05 14:39:06 +08:00
|
|
|
}
|
|
|
|
return float64(f), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToString ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:38 2023/5/5
|
2023-05-05 16:07:42 +08:00
|
|
|
func (f Float) ToString() (string, error) {
|
|
|
|
floatVal, err := f.ToFloat64()
|
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v", floatVal), nil
|
2023-05-05 14:39:06 +08:00
|
|
|
}
|