Skip to content

Commit

Permalink
[type:feat]enhance stringx ToInt & ToFloat64 (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzwei authored Aug 7, 2022
1 parent 768f91b commit c7ed568
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
20 changes: 16 additions & 4 deletions gox/stringsx/stringsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,33 @@ func DefaultIfEmpty(str string, defaultStr string) string {
// ToInt convert string int
// wrap strconv.Atoi
func ToInt(str string) int {
return ToIntOrDefault(str, 0)
}

// ToIntOrDefault convert string int, if str empty or err you can get default value tha you want.
// wrap strconv.Atoi
func ToIntOrDefault(str string, defVal int) int {
if Empty(str) {
return 0
return defVal
}
value, err := strconv.Atoi(str)
return defaultx.DefaultIntIfError(err, value, 0)
return defaultx.DefaultIntIfError(err, value, defVal)
}

// ToFloat64 convert string to float64
// wrap strconv.ParseFloat
func ToFloat64(str string) float64 {
return ToFloat64OrDefault(str, 0.0)
}

// ToFloat64OrDefault convert string to float64, if str empty or err you can get default value tha you want.
// wrap strconv.ParseFloat
func ToFloat64OrDefault(str string, defVal float64) float64 {
if Empty(str) {
return 0.0
return defVal
}
value, err := strconv.ParseFloat(str, 64)
return defaultx.DefaultFloat64IfError(err, value, 0.0)
return defaultx.DefaultFloat64IfError(err, value, defVal)
}

// ToBool convert string to bool
Expand Down
68 changes: 68 additions & 0 deletions gox/stringsx/stringsx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,38 @@ func TestToInt(t *testing.T) {
}
}

func TestToIntOrDefault(t *testing.T) {
type args struct {
str string
defVal int
}
tests := []struct {
name string
args args
want int
}{
{
args: args{str: "123"},
want: 123,
},
{
args: args{str: "", defVal: 1},
want: 1,
},
{
args: args{str: "abc", defVal: 2},
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToIntOrDefault(tt.args.str, tt.args.defVal); got != tt.want {
t.Errorf("ToIntOrDefault() = %v, want %v", got, tt.want)
}
})
}
}

func TestToBytes(t *testing.T) {
type args struct {
str string
Expand Down Expand Up @@ -224,6 +256,42 @@ func TestToFloat64(t *testing.T) {
}
}

func TestToFloat64OrDefault(t *testing.T) {
type args struct {
str string
defVal float64
}
tests := []struct {
name string
args args
want float64
}{
{
args: args{str: "0.001"},
want: 0.001,
},
{
args: args{str: ""},
want: 0.00,
},
{
args: args{str: "", defVal: 0.09},
want: 0.09,
},
{
args: args{str: "abc", defVal: 1.23},
want: 1.23,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToFloat64OrDefault(tt.args.str, tt.args.defVal); got != tt.want {
t.Errorf("ToFloat64OrDefault() = %v, want %v", got, tt.want)
}
})
}
}

func TestToBool(t *testing.T) {
type args struct {
str string
Expand Down

0 comments on commit c7ed568

Please sign in to comment.