diff --git a/decimal_test.go b/decimal_test.go index 25d95bc..cdb7997 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -3962,3 +3962,82 @@ func ExampleNewFromFloat() { //0.123123123123123 //-10000000000000 } + +func TestDecimal_RoundBank(t *testing.T) { + type testData struct { + value string + places int32 + expected string + } + + tests := []testData{ + {"2.5", 0, "2"}, + {"3.5", 0, "4"}, + {"4.5", 0, "4"}, + {"5.5", 0, "6"}, + {"2.45", 1, "2.4"}, + {"2.55", 1, "2.6"}, + {"1.45", 1, "1.4"}, + {"1.55", 1, "1.6"}, + {"1.65", 1, "1.6"}, + {"-2.5", 0, "-2"}, + {"-3.5", 0, "-4"}, + {"-4.5", 0, "-4"}, + {"-5.5", 0, "-6"}, + {"-2.45", 1, "-2.4"}, + {"-2.55", 1, "-2.6"}, + {"-1.45", 1, "-1.4"}, + {"-1.55", 1, "-1.6"}, + {"-1.65", 1, "-1.6"}, + {"2.4", 0, "2"}, + {"2.6", 0, "3"}, + {"2.499", 0, "2"}, + {"2.501", 0, "3"}, + {"-2.4", 0, "-2"}, + {"-2.6", 0, "-3"}, + {"-2.499", 0, "-2"}, + {"-2.501", 0, "-3"}, + {"0", 0, "0"}, + {"0.0", 0, "0"}, + {"0.5", 0, "0"}, + {"1.5", 0, "2"}, + {"-0.5", 0, "0"}, + {"-1.5", 0, "-2"}, + {"2.345", 2, "2.34"}, + {"2.355", 2, "2.36"}, + {"2.365", 2, "2.36"}, + {"-2.345", 2, "-2.34"}, + {"-2.355", 2, "-2.36"}, + {"-2.365", 2, "-2.36"}, + {"123.5", 0, "124"}, + {"124.5", 0, "124"}, + {"125.5", 0, "126"}, + {"545", -1, "540"}, + {"555", -1, "560"}, + {"565", -1, "560"}, + {"1250.0", -2, "1200"}, + {"1350.0", -2, "1400"}, + {"3.1415926535", 5, "3.14159"}, + {"3.1415926535", 4, "3.1416"}, + {"3.14155", 4, "3.1416"}, + {"3.14145", 4, "3.1414"}, + } + + for _, test := range tests { + d, err := NewFromString(test.value) + if err != nil { + t.Fatal(err) + } + + result := d.RoundBank(test.places) + expected, err := NewFromString(test.expected) + if err != nil { + t.Fatal(err) + } + + if !result.Equal(expected) { + t.Errorf("RoundBank(%s, %d): expected %s, got %s", + test.value, test.places, test.expected, result.String()) + } + } +}