Last active
August 11, 2021 12:34
-
-
Save mahmoud-eskandari/0df150a53c9d7a33e23ea2d3a6f09946 to your computer and use it in GitHub Desktop.
Number format without array reverse in golang
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 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 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NumberFormat ("1234567`") // Out: 1,234,567NumberFormat ("123456`") // Out: 123,456