Skip to content

Instantly share code, notes, and snippets.

@dbt4u
Created March 22, 2018 21:33
Show Gist options
  • Save dbt4u/cb5df47c9f10baa0193544afc44d7120 to your computer and use it in GitHub Desktop.
Save dbt4u/cb5df47c9f10baa0193544afc44d7120 to your computer and use it in GitHub Desktop.
Simple formater and converter Int64 into String with 1000er Separator
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