Compare commits

...

5 commits

Author SHA1 Message Date
Sergey Vasiliev
7bdd0f96cc
Merge 6e54f647f0 into d00399e161 2024-02-19 21:37:10 -07:00
Dovydas
d00399e161
Add NewFromBigRat constructor (#288) 2024-02-19 23:13:43 +01:00
vasilbekk
6e54f647f0 Newline in end file 2022-10-01 14:20:16 +03:00
vasilbekk
d23c457004 Added unittests 2022-10-01 14:02:56 +03:00
vasilbekk
ba54fe1418 Added varible StringTrimTrailingZeros and comment for it 2022-10-01 13:47:11 +03:00
2 changed files with 119 additions and 1 deletions

View file

@ -51,6 +51,14 @@ var DivisionPrecision = 16
// silently lose precision.
var MarshalJSONWithoutQuotes = false
// StringTrimTrailingZeros should be set to false if you want the decimal stringify without zeros trailing.
// By default, when decimal is output as a string (for example, in JSON), zeros are truncated from it (2.00 -> 2, 3.11 -> 3.11, 13.000 -> 13).
// But this logic can be changed by this variable.
// For example, if you have numeric(10,2) values stored in your database,
// and you want your API response to always be given 2 decimal places (even 2.00, 3.00, 17.00 [not 2,3,17]),
// then this variable is a great way out.
var StringTrimTrailingZeros = true
// ExpMaxIterations specifies the maximum number of iterations needed to calculate
// precise natural exponent value using ExpHullAbrham method.
var ExpMaxIterations = 1000
@ -124,6 +132,26 @@ func NewFromBigInt(value *big.Int, exp int32) Decimal {
}
}
// NewFromBigRat returns a new Decimal from a big.Rat. The numerator and
// denominator are divided and rounded to the given precision.
//
// Example:
//
// d1 := NewFromBigRat(big.NewRat(0, 1), 0) // output: "0"
// d2 := NewFromBigRat(big.NewRat(4, 5), 1) // output: "0.8"
// d3 := NewFromBigRat(big.NewRat(1000, 3), 3) // output: "333.333"
// d4 := NewFromBigRat(big.NewRat(2, 7), 4) // output: "0.2857"
//
func NewFromBigRat(value *big.Rat, precision int32) Decimal {
return Decimal{
value: new(big.Int).Set(value.Num()),
exp: 0,
}.DivRound(Decimal{
value: new(big.Int).Set(value.Denom()),
exp: 0,
}, precision)
}
// NewFromString returns a new Decimal from a string representation.
// Trailing zeroes are not trimmed.
//
@ -1142,7 +1170,7 @@ func (d Decimal) InexactFloat64() float64 {
//
// -12.345
func (d Decimal) String() string {
return d.string(true)
return d.string(StringTrimTrailingZeros)
}
// StringFixed returns a rounded fixed-point string with places digits after

View file

@ -556,6 +556,51 @@ func TestNewFromBigIntWithExponent(t *testing.T) {
}
}
func TestNewFromBigRat(t *testing.T) {
mustParseRat := func(val string) *big.Rat {
num, _ := new(big.Rat).SetString(val)
return num
}
type Inp struct {
val *big.Rat
prec int32
}
tests := map[Inp]string{
Inp{big.NewRat(0, 1), 16}: "0",
Inp{big.NewRat(4, 5), 16}: "0.8",
Inp{big.NewRat(10, 2), 16}: "5",
Inp{big.NewRat(1023427554493, 43432632), 16}: "23563.5628642767953828", // rounded
Inp{big.NewRat(1, 434324545566634), 16}: "0.0000000000000023",
Inp{big.NewRat(1, 3), 16}: "0.3333333333333333",
Inp{big.NewRat(2, 3), 2}: "0.67", // rounded
Inp{big.NewRat(2, 3), 16}: "0.6666666666666667", // rounded
Inp{big.NewRat(10000, 3), 16}: "3333.3333333333333333",
Inp{mustParseRat("30702832066636633479"), 16}: "30702832066636633479",
Inp{mustParseRat("487028320159896636679.1827512895753"), 16}: "487028320159896636679.1827512895753",
Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), -2}: "127028320612589896636633500", // rounded
Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), 16}: "127028320612589896636633479.1735827512895753", // rounded
Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), 32}: "127028320612589896636633479.173582751289575278357832",
}
// add negatives
for p, s := range tests {
if p.val.Cmp(new(big.Rat)) > 0 {
tests[Inp{p.val.Neg(p.val), p.prec}] = "-" + s
}
}
for input, s := range tests {
d := NewFromBigRat(input.val, input.prec)
if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
d.value.String(), d.exp)
}
}
}
func TestCopy(t *testing.T) {
origin := New(1, 0)
cpy := origin.Copy()
@ -3407,3 +3452,48 @@ func ExampleNewFromFloat() {
//0.123123123123123
//-10000000000000
}
func TestDecimal_String(t *testing.T) {
type testData struct {
input string
expected string
}
tests := []testData{
{"1.22", "1.22"},
{"1.00", "1"},
{"153.192", "153.192"},
{"999.999", "999.999"},
{"0.0000000001", "0.0000000001"},
{"0.0000000000", "0"},
}
for _, test := range tests {
d, err := NewFromString(test.input);
if err != nil {
t.Fatal(err)
} else if d.String() != test.expected {
t.Errorf("expected %s, got %s", test.expected, d.String())
}
}
defer func() {
StringTrimTrailingZeros = true
}()
StringTrimTrailingZeros = false
tests = []testData{
{"1.00", "1.00"},
{"0.00", "0.00"},
{"129.123000", "129.123000"},
}
for _, test := range tests {
d, err := NewFromString(test.input);
if err != nil {
t.Fatal(err)
} else if d.String() != test.expected {
t.Errorf("expected %s, got %s", test.expected, d.String())
}
}
}