-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional string converters (#1088)
- Loading branch information
Showing
8 changed files
with
236 additions
and
116 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package converters | ||
|
||
import "strconv" | ||
|
||
func Float64ToString(value float64) string { | ||
return strconv.FormatFloat(value, 'f', -1, 64) | ||
} | ||
|
||
func Float32ToString(value float32) string { | ||
return strconv.FormatFloat(float64(value), 'f', -1, 32) | ||
} | ||
|
||
func BooleanToBit(val bool) int { | ||
if val { | ||
return 1 | ||
} else { | ||
return 0 | ||
} | ||
} |
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,68 @@ | ||
package converters | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBooleanToBit(t *testing.T) { | ||
assert.Equal(t, 1, BooleanToBit(true)) | ||
assert.Equal(t, 0, BooleanToBit(false)) | ||
} | ||
|
||
func TestFloat32ToString(t *testing.T) { | ||
type ioPair struct { | ||
input float32 | ||
output string | ||
} | ||
|
||
ioPairs := []ioPair{ | ||
{123.456, "123.456"}, | ||
{0.0, "0"}, | ||
{-1.0, "-1"}, | ||
{1.0, "1"}, | ||
{340282350000000000000000000000000000000, "340282350000000000000000000000000000000"}, | ||
{math.MaxFloat32, "340282350000000000000000000000000000000"}, | ||
{0.000000000000000000000000000000000000000000001, "0.000000000000000000000000000000000000000000001"}, | ||
{-340282350000000000000000000000000000000, "-340282350000000000000000000000000000000"}, | ||
{1.401298464324817070923729583289916131280e-45, "0.000000000000000000000000000000000000000000001"}, | ||
{1.17549435e-38, "0.000000000000000000000000000000000000011754944"}, | ||
{-1.17549435e-38, "-0.000000000000000000000000000000000000011754944"}, | ||
{2.71828, "2.71828"}, | ||
{-2.71828, "-2.71828"}, | ||
{3.14159, "3.14159"}, | ||
{-3.14159, "-3.14159"}, | ||
} | ||
|
||
for _, pair := range ioPairs { | ||
assert.Equal(t, pair.output, Float32ToString(pair.input), pair.input) | ||
} | ||
} | ||
|
||
func TestFloat64ToString(t *testing.T) { | ||
type ioPair struct { | ||
input float64 | ||
output string | ||
} | ||
|
||
ioPairs := []ioPair{ | ||
{123.456, "123.456"}, | ||
{0.0, "0"}, | ||
{-1.0, "-1"}, | ||
{1.0, "1"}, | ||
{1.7976931348623157e+308, "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}, | ||
{math.MaxFloat64, "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}, | ||
{4.9406564584124654e-324, "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005"}, | ||
{-1.7976931348623157e+308, "-179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}, | ||
{2.718281828459045, "2.718281828459045"}, | ||
{-2.718281828459045, "-2.718281828459045"}, | ||
{3.141592653589793, "3.141592653589793"}, | ||
{-3.141592653589793, "-3.141592653589793"}, | ||
} | ||
|
||
for _, pair := range ioPairs { | ||
assert.Equal(t, pair.output, Float64ToString(pair.input), pair.input) | ||
} | ||
} |
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
Oops, something went wrong.