30 lines
556 B
Go
30 lines
556 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"go.uber.org/zap/buffer"
|
|
)
|
|
|
|
func addCommas(n int) string {
|
|
str := strconv.Itoa(n)
|
|
|
|
// Add commas from right to left
|
|
var result buffer.Buffer
|
|
for i := range str {
|
|
if i > 0 && (len(str)-i)%3 == 0 {
|
|
fmt.Fprint(&result,",")
|
|
}
|
|
result.WriteByte(str[i])
|
|
}
|
|
|
|
return result.String()
|
|
}
|
|
|
|
func main() {
|
|
numbers := []int{12345, 1234567, 987654321, 12345, 123, 0}
|
|
|
|
for _, num := range numbers {
|
|
fmt.Printf("%d -> %s\n", num, addCommas(num))
|
|
}
|
|
} |