Skip to content

Commit

Permalink
Replace :half-up rounding mode with :half-even
Browse files Browse the repository at this point in the history
RoundingMode.HALF_EVEN is preferred over RoundingMode.HALF_UP to
minimize rounding bias in repeated calculations.
  • Loading branch information
sernamar committed Sep 26, 2024
1 parent f336596 commit ec9fad6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions dev/arithmetic_operations_simulation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
(defn rounded-money-simulation
[money]
(-> money
(core/add (core/rounded-money-of 1234567.3444 :eur 2 :half-up))
(core/subtract (core/rounded-money-of 232323 :eur 2 :half-up))
(core/add (core/rounded-money-of 1234567.3444 :eur 2 :half-even))
(core/subtract (core/rounded-money-of 232323 :eur 2 :half-even))
(core/multiply 3.4)
(core/divide 5.456)))

(time
(reduce (fn [acc _] (rounded-money-simulation acc)) (core/rounded-money-of 0 :eur 2 :half-up) (range 1000000)))
(reduce (fn [acc _] (rounded-money-simulation acc)) (core/rounded-money-of 0 :eur 2 :half-even) (range 1000000)))
;; => "Elapsed time: 7031.994195 msecs"
;; => {:amount 1657407.95M, :currency :eur, :scale 2, :rounding-mode :half-up}
;; => {:amount 1657407.95M, :currency :eur, :scale 2, :rounding-mode :half-even}
12 changes: 6 additions & 6 deletions test/dinero/format_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
m2 (core/money-of 1 :btc)
germany Locale/GERMANY]
(t/is (= "1.234,57 €" (sut/format-money m1 {:locale germany
:rounding-mode :half-up
:rounding-mode :half-even
:decimal-places 2})))
(t/is (= "1.234,56 €" (sut/format-money m1 {:locale germany
:rounding-mode :down
Expand All @@ -39,19 +39,19 @@
:rounding-mode :down
:decimal-places 0})))
(t/is (= "1.234,57 €" (sut/format-money m1 {:locale germany
:rounding-mode :half-up
:rounding-mode :half-even
:decimal-places 2
:symbol-style :symbol})))
(t/is (= "1.234,57 EUR" (sut/format-money m1 {:locale germany
:rounding-mode :half-up
:rounding-mode :half-even
:decimal-places 2
:symbol-style :code})))
(t/is (= "1,00 ₿" (sut/format-money m2 {:locale germany
:rounding-mode :half-up
:rounding-mode :half-even
:decimal-places 2
:symbol-style :symbol})))
(t/is (= "1,00 BTC" (sut/format-money m2 {:locale germany
:rounding-mode :half-up
:rounding-mode :half-even
:decimal-places 2
:symbol-style :code})))))

Expand All @@ -65,5 +65,5 @@
(t/is (= "1.234,568 €" (sut/format-money-with-pattern money "#,##0.000 ¤" {:locale germany})))
(t/is (= "1,234.57 £" (sut/format-money-with-pattern money "#,##0.00 ¤" {:locale uk})))
(t/is (= "1,234.57 GBP" (sut/format-money-with-pattern money "#,##0.00 ¤¤" {:locale uk})))
(t/is (= "1.234,57 €" (sut/format-money-with-pattern money "#,##0.00 ¤" {:locale germany :rounding-mode :half-up})))
(t/is (= "1.234,57 €" (sut/format-money-with-pattern money "#,##0.00 ¤" {:locale germany :rounding-mode :half-even})))
(t/is (= "1.234,56 €" (sut/format-money-with-pattern money "#,##0.00 ¤" {:locale germany :rounding-mode :down})))))

0 comments on commit ec9fad6

Please sign in to comment.