-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: conversion type from float to int
- Loading branch information
Showing
2 changed files
with
81 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package god | ||
|
||
import "reflect" | ||
|
||
type Numeric interface { | ||
ToFloat64() float64 | ||
ToFloat32() float32 | ||
ToInt64() int64 | ||
ToInt32() int | ||
} | ||
|
||
func ConvertToNumeric(value interface{}) (Numeric, bool) { | ||
v := reflect.ValueOf(value) | ||
if v.Kind() == reflect.Ptr { | ||
elemType := v.Elem().Type() | ||
|
||
switch elemType.Kind() { | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return MyInt(v.Elem().Int()), true | ||
case reflect.Float32, reflect.Float64: | ||
return MyFloat64(v.Elem().Float()), true | ||
} | ||
} else { | ||
switch v.Kind() { | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return MyInt(v.Int()), true | ||
case reflect.Float32, reflect.Float64: | ||
return MyFloat64(v.Float()), true | ||
} | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
type MyInt int | ||
|
||
func (mi MyInt) ToFloat64() float64 { | ||
return float64(mi) | ||
} | ||
|
||
func (mf MyInt) ToFloat32() float32 { | ||
return float32(mf) | ||
} | ||
|
||
func (mi MyInt) ToInt32() int { | ||
return int(mi) | ||
} | ||
|
||
func (mi MyInt) ToInt64() int64 { | ||
return int64(mi) | ||
} | ||
|
||
type MyFloat64 float64 | ||
|
||
func (mf MyFloat64) ToFloat64() float64 { | ||
return float64(mf) | ||
} | ||
|
||
func (mf MyFloat64) ToFloat32() float32 { | ||
return float32(mf) | ||
} | ||
|
||
func (mf MyFloat64) ToInt32() int { | ||
return int(mf) | ||
} | ||
|
||
func (mi MyFloat64) ToInt64() int64 { | ||
return int64(mi) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters