Created
March 22, 2018 21:33
-
-
Save dbt4u/cb5df47c9f10baa0193544afc44d7120 to your computer and use it in GitHub Desktop.
Simple formater and converter Int64 into String with 1000er Separator
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
func format(n int64) string { | |
in := strconv.FormatInt(n, 10) | |
out := make([]byte, len(in)+(len(in)-2+int(in[0]/'0'))/3) | |
if in[0] == '-' { | |
in, out[0] = in[1:], '-' | |
} | |
for i, j, k := len(in)-1, len(out)-1, 0; ; i, j = i-1, j-1 { | |
out[j] = in[i] | |
if i == 0 { | |
return string(out) | |
} | |
if k++; k == 3 { | |
j, k = j-1, 0 | |
out[j] = ',' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment