From fab0fa458cb21d46fe71e056ea65ee7255dcaf1f Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Wed, 23 Apr 2025 16:46:39 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20StringIn?= =?UTF-8?q?tWithSeparator()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- micro/microfunctions.go | 38 ++++++++++++++++++++++++++++++++++++ micro/microfunctions_test.go | 9 +++++++++ 2 files changed, 47 insertions(+) diff --git a/micro/microfunctions.go b/micro/microfunctions.go index edd020c9..fb9061c7 100644 --- a/micro/microfunctions.go +++ b/micro/microfunctions.go @@ -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() +} diff --git a/micro/microfunctions_test.go b/micro/microfunctions_test.go index 219f7941..9b14d6f9 100644 --- a/micro/microfunctions_test.go +++ b/micro/microfunctions_test.go @@ -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() + } +}