Updated RoundingMode config

This commit is contained in:
Trial97
2021-12-16 11:28:19 +02:00
committed by Dan Christian Bogos
parent 868f1d630c
commit 94c4d0ffc1
7 changed files with 46 additions and 16 deletions

View File

@@ -56,7 +56,7 @@ const CGRATES_CFG_JSON = `
"decimal_max_scale": 0, // the maximum scale for decimal numbers
"decimal_min_scale": 0, // the minimum scale for decimal numbers
"decimal_precision": 0, // the precision of the decimal operations
"decimal_rounding_mode": "ToNearestEven", // the rounding mode <ToNearestEvend|ToNearestAwayd|ToZerod|AwayFromZerod|ToNegativeInfd|ToPositiveInfd|ToNearestTowardZero>
"decimal_rounding_mode": "*toNearestEven", // the rounding mode <*toNearestEven|*toNearestAway|*toZero|*awayFromZero|*toNegativeInf|*toPositiveInf|*toNearestTowardZero>
},

View File

@@ -57,7 +57,7 @@ func TestDfGeneralJsonCfg(t *testing.T) {
Decimal_max_scale: utils.IntPointer(0),
Decimal_min_scale: utils.IntPointer(0),
Decimal_precision: utils.IntPointer(0),
Decimal_rounding_mode: utils.StringPointer("ToNearestEven"),
Decimal_rounding_mode: utils.StringPointer("*toNearestEven"),
}
dfCgrJSONCfg, err := NewCgrJsonCfgFromBytes([]byte(CGRATES_CFG_JSON))
if err != nil {

File diff suppressed because one or more lines are too long

View File

@@ -196,7 +196,7 @@ func (gencfg GeneralCfg) AsMapInterface(string) interface{} {
utils.DecimalMaxScaleCfg: gencfg.DecimalMaxScale,
utils.DecimalMinScaleCfg: gencfg.DecimalMinScale,
utils.DecimalPrecisionCfg: gencfg.DecimalPrecision,
utils.DecimalRoundingModeCfg: gencfg.DecimalRoundingMode.String(),
utils.DecimalRoundingModeCfg: utils.RoundingModeToString(gencfg.DecimalRoundingMode),
}
if gencfg.LockingTimeout != 0 {

View File

@@ -173,7 +173,7 @@ func TestGeneralCfgAsMapInterface(t *testing.T) {
utils.DecimalMaxScaleCfg: 0,
utils.DecimalMinScaleCfg: 0,
utils.DecimalPrecisionCfg: 0,
utils.DecimalRoundingModeCfg: "ToNearestEven",
utils.DecimalRoundingModeCfg: "*toNearestEven",
}
if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgJSONStr); err != nil {
t.Error(err)
@@ -220,7 +220,7 @@ func TestGeneralCfgAsMapInterface1(t *testing.T) {
utils.DecimalMaxScaleCfg: 0,
utils.DecimalMinScaleCfg: 0,
utils.DecimalPrecisionCfg: 0,
utils.DecimalRoundingModeCfg: "ToNearestEven",
utils.DecimalRoundingModeCfg: "*toNearestEven",
}
if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgJSONStr); err != nil {
t.Error(err)

View File

@@ -2618,6 +2618,17 @@ const (
HSuffix = "h"
)
// rounding strings
const (
ToNearestEven = "*toNearestEven"
ToNearestAway = "*toNearestAway"
ToZero = "*toZero"
AwayFromZero = "*awayFromZero"
ToNegativeInf = "*toNegativeInf"
ToPositiveInf = "*toPositiveInf"
ToNearestTowardZero = "*toNearestTowardZero"
)
func buildCacheInstRevPrefixes() {
CachePrefixToInstance = make(map[string]string)
for k, v := range CacheInstanceToPrefix {

View File

@@ -40,25 +40,44 @@ func init() {
func NewRoundingMode(rnd string) (decimal.RoundingMode, error) {
switch rnd {
case decimal.ToNearestEven.String():
case ToNearestEven:
return decimal.ToNearestEven, nil
case decimal.ToNearestAway.String():
case ToNearestAway:
return decimal.ToNearestAway, nil
case decimal.ToZero.String():
case ToZero:
return decimal.ToZero, nil
case decimal.AwayFromZero.String():
case AwayFromZero:
return decimal.AwayFromZero, nil
case decimal.ToNegativeInf.String():
case ToNegativeInf:
return decimal.ToNegativeInf, nil
case decimal.ToPositiveInf.String():
case ToPositiveInf:
return decimal.ToPositiveInf, nil
case decimal.ToNearestTowardZero.String():
case ToNearestTowardZero:
return decimal.ToNearestTowardZero, nil
default:
return 7, fmt.Errorf("usupoorted rounding: <%q>", rnd)
}
}
func RoundingModeToString(rnd decimal.RoundingMode) string {
switch rnd {
case decimal.ToNearestEven:
return ToNearestEven
case decimal.ToNearestAway:
return ToNearestAway
case decimal.ToZero:
return ToZero
case decimal.AwayFromZero:
return AwayFromZero
case decimal.ToNegativeInf:
return ToNegativeInf
case decimal.ToPositiveInf:
return ToPositiveInf
case decimal.ToNearestTowardZero:
return ToNearestTowardZero
default:
return EmptyString
}
}
func DivideBig(x, y *decimal.Big) *decimal.Big {
if x == nil || y == nil {
return nil