Skip to content

Instantly share code, notes, and snippets.

@mahmoud-eskandari
Last active August 11, 2021 12:34
Show Gist options
  • Select an option

  • Save mahmoud-eskandari/0df150a53c9d7a33e23ea2d3a6f09946 to your computer and use it in GitHub Desktop.

Select an option

Save mahmoud-eskandari/0df150a53c9d7a33e23ea2d3a6f09946 to your computer and use it in GitHub Desktop.
Number format without array reverse in golang
func NumberFormat(s string) (out string) {
ln := len(s)
mod := ln % 3
m := strings.Split(s, "")
for k, v := range m {
if (mod == 0 && k > 0 && k%3 == 0 && k < ln) ||
(mod > 0 && k >= (mod) && (k+(3-mod))%3 == 0 && k < ln) {
out += ","
}
out += v
}
return
}
@mahmoud-eskandari
Copy link
Author

NumberFormat ("1234567`") // Out: 1,234,567
NumberFormat ("123456`") // Out: 123,456

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment