-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29e1ff8
commit 7a23bdc
Showing
3 changed files
with
36 additions
and
15 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 |
---|---|---|
|
@@ -23,12 +23,27 @@ func ToJSON(obj interface{}) (string, error) { | |
} | ||
|
||
// ToFloat convert the input string to a float, or 0.0 if the input is not a float. | ||
func ToFloat(str string) (float64, error) { | ||
res, err := strconv.ParseFloat(str, 64) | ||
if err != nil { | ||
res = 0.0 | ||
func ToFloat(value interface{}) (res float64, err error) { | ||
val := reflect.ValueOf(value) | ||
|
||
switch value.(type) { | ||
case int, int8, int16, int32, int64: | ||
res = float64(val.Int()) | ||
case uint, uint8, uint16, uint32, uint64: | ||
res = float64(val.Uint()) | ||
case float32, float64: | ||
res = val.Float() | ||
case string: | ||
res, err = strconv.ParseFloat(val.String(), 64) | ||
if err != nil { | ||
res = 0 | ||
} | ||
default: | ||
err = fmt.Errorf("ToInt: unknown interface type %T", value) | ||
res = 0 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Yaddsld5
|
||
} | ||
return res, err | ||
|
||
return | ||
} | ||
|
||
// ToInt convert the input string or any int type to an integer type 64, or 0 if the input is not an integer. | ||
|
@@ -40,6 +55,8 @@ func ToInt(value interface{}) (res int64, err error) { | |
res = val.Int() | ||
case uint, uint8, uint16, uint32, uint64: | ||
res = int64(val.Uint()) | ||
case float32, float64: | ||
res = int64(val.Float()) | ||
case string: | ||
if IsInt(val.String()) { | ||
res, err = strconv.ParseInt(val.String(), 0, 64) | ||
|
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
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
1 comment
on commit 7a23bdc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
[ ]
-
[ ]
ISO3166Alpha2