parent
e60c20bc10
commit
e1d2b9b1c1
|
@ -64,10 +64,10 @@
|
||||||
Example usage:
|
Example usage:
|
||||||
(convert-to-whole-number 12345 2) ; => 123.45"
|
(convert-to-whole-number 12345 2) ; => 123.45"
|
||||||
[amount decimals]
|
[amount decimals]
|
||||||
(-> amount
|
(when-let [[amount-bn divisor-bn] (money/->bignumbers amount (money/from-decimal decimals))]
|
||||||
(/ (Math/pow 10 decimals))
|
(-> (money/div amount-bn divisor-bn)
|
||||||
(.toFixed decimals)
|
(money/to-fixed decimals)
|
||||||
remove-trailing-zeroes))
|
remove-trailing-zeroes)))
|
||||||
|
|
||||||
(defn hex->whole
|
(defn hex->whole
|
||||||
[num decimals]
|
[num decimals]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
(ns utils.number-test
|
(ns utils.number-test
|
||||||
(:require
|
(:require
|
||||||
[cljs.test :refer [deftest is testing]]
|
[cljs.test :refer [deftest is testing]]
|
||||||
|
[utils.money :as money]
|
||||||
[utils.number]))
|
[utils.number]))
|
||||||
|
|
||||||
(deftest convert-to-whole-number-test
|
(deftest convert-to-whole-number-test
|
||||||
|
@ -21,7 +22,11 @@
|
||||||
|
|
||||||
(testing "handles zero amount"
|
(testing "handles zero amount"
|
||||||
(is (= "0" (utils.number/convert-to-whole-number 0 2)))
|
(is (= "0" (utils.number/convert-to-whole-number 0 2)))
|
||||||
(is (= "0" (utils.number/convert-to-whole-number 0 0)))))
|
(is (= "0" (utils.number/convert-to-whole-number 0 0))))
|
||||||
|
|
||||||
|
(testing "handles BigNumber amounts with 18 decimals"
|
||||||
|
(let [amount (money/bignumber "3141969777175276657")]
|
||||||
|
(is (= "3.141969777175276657" (utils.number/convert-to-whole-number amount 18))))))
|
||||||
|
|
||||||
(deftest parse-int-test
|
(deftest parse-int-test
|
||||||
(testing "defaults to zero"
|
(testing "defaults to zero"
|
||||||
|
|
Loading…
Reference in New Issue