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

сделал RoundFloat64()

This commit is contained in:
Nikitin Aleksandr
2025-04-28 17:50:36 +03:00
parent 9934b3f02f
commit 7121601717
2 changed files with 17 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ import (
"golang.org/x/exp/constraints"
"google.golang.org/protobuf/types/known/timestamppb"
"hash/fnv"
"math"
"os/exec"
"reflect"
"runtime"
@@ -1767,3 +1768,12 @@ func StringIntWithSeparator(n int, separator rune) string {
return buff.String()
}
// RoundFloat64 - округляет float64 до precision цифр после запятой
// пример:
// RoundFloat64(123.456, 2) = 123.46
// RoundFloat64(123.456, 1) = 123.5
func RoundFloat64(value float64, precision int) float64 {
ratio := math.Pow(10, float64(precision))
return math.Round(value*ratio) / ratio
}