Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String float to number #170

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"html/template"
"math"
"path"
"reflect"
"testing"
Expand All @@ -34,11 +35,16 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp
isUint := kind == reflect.Uint || kind == reflect.Uint8 || kind == reflect.Uint16 || kind == reflect.Uint32 || kind == reflect.Uint64

// Some precision is lost when converting from float64 to float32.
// rounded variables are necessary for string parsed value comparison
eightpoint31_32 := eightpoint31
eightpoint31_32_rounded := eightpoint31
eightpoint31negative_32 := eightpoint31negative
eightpoint31negative_32_rounded := eightpoint31negative
if kind == reflect.Float64 {
eightpoint31_32 = float64(float32(eightpoint31.(float64)))
eightpoint31_32_rounded = math.Floor(float64(float32(eightpoint31.(float64)))*100) / 100
eightpoint31negative_32 = float64(float32(eightpoint31negative.(float64)))
eightpoint31negative_32_rounded = math.Ceil(float64(float32(eightpoint31negative.(float64)))*100) / 100
}

return []testStep{
Expand All @@ -56,6 +62,8 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp
{uint64(8), eight, false},
{float32(8.31), eightpoint31_32, false},
{float64(8.31), eightpoint31, false},
{"8.31", eightpoint31_32_rounded, false},
{"8.31", eightpoint31, false},
{true, one, false},
{false, zero, false},
{"8", eight, false},
Expand All @@ -67,6 +75,8 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp
{int64(-8), eightnegative, isUint},
{float32(-8.31), eightpoint31negative_32, isUint},
{float64(-8.31), eightpoint31negative, isUint},
{"-8.31", eightpoint31negative_32_rounded, isUint},
{"-8.31", eightpoint31negative, isUint},
{"-8", eightnegative, isUint},
{jeight, eight, false},
{jminuseight, eightnegative, isUint},
Expand Down
34 changes: 23 additions & 11 deletions caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func ToTimeInDefaultLocationE(i interface{}, location *time.Location) (tim time.
case string:
return StringToDateInDefaultLocation(v, location)
case json.Number:
s, err1 := ToInt64E(v)
// Originally this used ToInt64E, but since it supports casting from string float
// the behavior of ToTime would have changed if we continued using it.
// For now, using json.Number's own Int64 method should be good enough.
s, err1 := v.Int64()
if err1 != nil {
return time.Time{}, fmt.Errorf("unable to cast %#v of type %T to Time", i, i)
}
Expand Down Expand Up @@ -260,7 +263,7 @@ func ToInt64E(i interface{}) (int64, error) {
case float32:
return int64(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimDecimal(s), 0, 0)
Comment on lines -263 to +266
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, it may be more correct to parse into float when a dot is present and convert to int after that.

if err == nil {
return v, nil
}
Expand Down Expand Up @@ -312,7 +315,7 @@ func ToInt32E(i interface{}) (int32, error) {
case float32:
return int32(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimDecimal(s), 0, 0)
if err == nil {
return int32(v), nil
}
Expand Down Expand Up @@ -364,7 +367,7 @@ func ToInt16E(i interface{}) (int16, error) {
case float32:
return int16(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimDecimal(s), 0, 0)
if err == nil {
return int16(v), nil
}
Expand Down Expand Up @@ -416,7 +419,7 @@ func ToInt8E(i interface{}) (int8, error) {
case float32:
return int8(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimDecimal(s), 0, 0)
if err == nil {
return int8(v), nil
}
Expand Down Expand Up @@ -468,7 +471,7 @@ func ToIntE(i interface{}) (int, error) {
case float32:
return int(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimDecimal(s), 0, 0)
if err == nil {
return int(v), nil
}
Expand Down Expand Up @@ -501,7 +504,7 @@ func ToUintE(i interface{}) (uint, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimDecimal(s), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -577,7 +580,7 @@ func ToUint64E(i interface{}) (uint64, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimDecimal(s), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -653,7 +656,7 @@ func ToUint32E(i interface{}) (uint32, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimDecimal(s), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -729,7 +732,7 @@ func ToUint16E(i interface{}) (uint16, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimDecimal(s), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -805,7 +808,7 @@ func ToUint8E(i interface{}) (uint8, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimDecimal(s), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -1458,6 +1461,15 @@ func toInt(v interface{}) (int, bool) {
}
}

func trimDecimal(s string) string {
// trim the decimal part (if any)
if i := strings.Index(s, "."); i >= 0 {
s = s[:i]
}

return s
}

func trimZeroDecimal(s string) string {
var foundZero bool
for i := len(s); i > 0; i-- {
Expand Down