76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
// 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() Float32Result {
|
|
res := Float32Result{
|
|
Value: 0,
|
|
Err: 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 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 14:36 2023/5/5
|
|
func (f Float) ToFloat64() Float64Result {
|
|
res := Float64Result{
|
|
Value: 0,
|
|
Err: 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 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 14:38 2023/5/5
|
|
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,
|
|
}
|
|
}
|