Last active
December 1, 2016 16:32
-
-
Save mishudark/c9bdb161c83b976ab0925176098c8073 to your computer and use it in GitHub Desktop.
Humanize numbers in go
This file contains hidden or 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
| package main | |
| import ( | |
| "fmt" | |
| "regexp" | |
| "strconv" | |
| ) | |
| func Currency(ammount interface{}) (string, error) { | |
| var ok bool | |
| var orig string | |
| var err error | |
| if orig, ok = ammount.(string); ok { | |
| if ammount, err = strconv.ParseFloat(orig, 64); err != nil { | |
| return "", err | |
| } | |
| } | |
| if _, ok := ammount.(float64); ok { | |
| ammountInt := int(ammount.(float64)) | |
| if ammount == float64(ammountInt) { | |
| ammount = ammountInt | |
| } else { | |
| orig = fmt.Sprintf("%f", ammount) | |
| } | |
| } | |
| if _, ok := ammount.(int); ok { | |
| orig = fmt.Sprintf("%d", ammount) | |
| } | |
| return comma(orig), nil | |
| } | |
| func comma(ammount string) string { | |
| re := regexp.MustCompile("^(-?[0-9]+)([0-9]{3})") | |
| nu := re.ReplaceAllString(ammount, "${1},${2}") | |
| if ammount == nu { | |
| fmt.Println(ammount) | |
| re = regexp.MustCompile("\\.([0-9]{1,2})([0-9]+)?") | |
| return re.ReplaceAllString(ammount, ".${1}") | |
| } | |
| return comma(nu) | |
| } |
This file contains hidden or 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
| package main | |
| import "testing" | |
| type testCase struct { | |
| currency interface{} | |
| converted string | |
| hasErr bool | |
| } | |
| func TestCurrency(t *testing.T) { | |
| tests := []testCase{ | |
| testCase{ | |
| currency: 123, | |
| converted: "123", | |
| }, | |
| testCase{ | |
| currency: 1123, | |
| converted: "1,123", | |
| }, | |
| testCase{ | |
| currency: 12312, | |
| converted: "12,312", | |
| }, | |
| testCase{ | |
| currency: 123123, | |
| converted: "123,123", | |
| }, | |
| testCase{ | |
| currency: 123123.1, | |
| converted: "123,123.10", | |
| }, | |
| testCase{ | |
| currency: 123123.12, | |
| converted: "123,123.12", | |
| }, | |
| testCase{ | |
| currency: 123123.00, | |
| converted: "123,123", | |
| }, | |
| testCase{ | |
| currency: "123123", | |
| converted: "123,123", | |
| }, | |
| testCase{ | |
| currency: "123123.00", | |
| converted: "123,123", | |
| }, | |
| } | |
| for _, test := range tests { | |
| val, err := Currency(test.currency) | |
| if test.hasErr == false && err != nil { | |
| t.Error("expected: nil, got:", err) | |
| } | |
| if val != test.converted { | |
| t.Errorf("expected: %s got: %s", test.converted, val) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment