mirror of
https://github.com/ManyakRus/starter.git
synced 2025-11-25 23:02:22 +02:00
сделал StringIntWithSeparator()
This commit is contained in:
@@ -1689,3 +1689,41 @@ func StringDatePeriod_rus(Date1, Date2 time.Time) string {
|
|||||||
|
|
||||||
return Otvet
|
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()
|
||||||
|
}
|
||||||
|
|||||||
@@ -1498,3 +1498,12 @@ func TestStringDateSPo_rus(t *testing.T) {
|
|||||||
t.Errorf("error: Expected not empty string, but got %s", Otvet)
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user