From 0159d248d70f6a314f099ceb37edd9c7d5fec092 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Wed, 20 Mar 2024 17:50:18 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20StringFl?= =?UTF-8?q?oat64=5FDimension2()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- micro/microfunctions.go | 14 +++++++++++++ micro/microfunctions_test.go | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/micro/microfunctions.go b/micro/microfunctions.go index 1ea90a2c..7599e347 100644 --- a/micro/microfunctions.go +++ b/micro/microfunctions.go @@ -808,3 +808,17 @@ func FindLastPos(s, TextFind string) int { Otvet := strings.LastIndex(s, TextFind) return Otvet } + +// StringFloat64_Dimension2 - возвращает строку с 2 знака после запятой +func StringFloat64_Dimension2(f float64) string { + Otvet := fmt.Sprintf("%.2f", f) + + return Otvet +} + +// StringFloat32_Dimension2 - возвращает строку с 2 знака после запятой +func StringFloat32_Dimension2(f float32) string { + Otvet := fmt.Sprintf("%.2f", f) + + return Otvet +} diff --git a/micro/microfunctions_test.go b/micro/microfunctions_test.go index 878d49e4..1014c757 100644 --- a/micro/microfunctions_test.go +++ b/micro/microfunctions_test.go @@ -554,3 +554,43 @@ func TestFindLastPos(t *testing.T) { t.Error("microfunctions_test.TestFindLastPos() FindLastPos()=nil !") } } + +func TestStringFloat64_Dimension2(t *testing.T) { + // Testing for a positive float number + result := StringFloat64_Dimension2(3.14159) + if result != "3.14" { + t.Errorf("Expected '3.14' but got %s", result) + } + + // Testing for a negative float number + result = StringFloat64_Dimension2(-123.456) + if result != "-123.46" { + t.Errorf("Expected '-123.46' but got %s", result) + } + + // Testing for zero + result = StringFloat64_Dimension2(0.0) + if result != "0.00" { + t.Errorf("Expected '0.00' but got %s", result) + } +} + +func TestStringFloat32_Dimension2(t *testing.T) { + // Testing for a positive float number + result := StringFloat32_Dimension2(3.14159) + if result != "3.14" { + t.Errorf("Expected '3.14' but got %s", result) + } + + // Testing for a negative float number + result = StringFloat32_Dimension2(-123.456) + if result != "-123.46" { + t.Errorf("Expected '-123.46' but got %s", result) + } + + // Testing for zero + result = StringFloat32_Dimension2(0.0) + if result != "0.00" { + t.Errorf("Expected '0.00' but got %s", result) + } +}