1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-23 22:45:11 +02:00

сделал StringIntWithSeparator()

This commit is contained in:
Nikitin Aleksandr
2025-04-23 16:46:39 +03:00
parent 7ea6b37df7
commit fab0fa458c
2 changed files with 47 additions and 0 deletions

View File

@@ -1689,3 +1689,41 @@ func StringDatePeriod_rus(Date1, Date2 time.Time) string {
return Otvet
}
// StringIntWithSeparator - возвращает строку с разделителем по 3 разрядам
// пример:
// s := StringIntWithSeparator(1222333, '_')
// Ответ: "1_222_333"
func StringIntWithSeparator(n int, separator rune) string {
s := strconv.Itoa(n)
startOffset := 0
var buff bytes.Buffer
if n < 0 {
startOffset = 1
buff.WriteByte('-')
}
l := len(s)
commaIndex := 3 - ((l - startOffset) % 3)
if commaIndex == 3 {
commaIndex = 0
}
for i := startOffset; i < l; i++ {
if commaIndex == 3 {
buff.WriteRune(separator)
commaIndex = 0
}
commaIndex++
buff.WriteByte(s[i])
}
return buff.String()
}

View File

@@ -1498,3 +1498,12 @@ func TestStringDateSPo_rus(t *testing.T) {
t.Errorf("error: Expected not empty string, but got %s", Otvet)
}
}
func TestStringIntWithSeparator(t *testing.T) {
x := 1222333
//var r rune = '_'
s := StringIntWithSeparator(x, '_')
if s == "" {
t.Error()
}
}