-
Notifications
You must be signed in to change notification settings - Fork 30
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
Additional string converters #1088
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8d6f16e
String converters.
Tang8330 580ec7b
Clean up.
Tang8330 a0bd0b0
Adding TODO.
Tang8330 d493246
Merge branch 'master' into string-converters
Tang8330 f7659d3
Merge branch 'master' into string-converters
Tang8330 39a530c
Imports.
Tang8330 de3270c
Clean up.
Tang8330 4cb0b91
Clean up.
Tang8330 b3cd738
Merge branch 'master' into additional-string-converters
Tang8330 ace5a06
Merge branch 'master' into additional-string-converters
Tang8330 2c64b7d
Merge branch 'master' into additional-string-converters
Tang8330 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved only |
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved only |
||
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This was pretty funny. We labeled
id
as an integer, but the values werepk-1
,pk-2
, etc.