Introduction

decimal is a small and concise library that provides Decimal type for clojurescript.

Warning

This documentation does not covers all api, so if you miss some function, contributions are very welcome. You can see the full API documentation here.

Project Maturity

Since decimal is a young project there can be some API breakage.

Install

The simplest way to use decimal in a clojurescript project, is by including it in the dependency vector on your project.clj file:

[funcool/decimal "1.0.2"]

Use

To start using it you have to require the decimal.core namespace.

(require '[decimal.core :as dc])

It accepts a value of type number, string or Decimal.

(let [x (dc/decimal 123.4567)
	  y (dc/decimal "123456.7e-3")
	  z (dc/decimal x)]
  (dc/= x y z))

A value can also be in binary, hexadecimal or octal if the appropriate prefix is included.

(let [x (dc/decimal "0xff.f")
	  y (dc/decimal "0b10101100")
	  z (dc/+ x y)]
  (dc/to-binary z)     ;; "0b110101011.1111"
  (dc/to-binary z 13)) ;; "0b1.101010111111p+8"

The methods that return a Decimal can be chained.

(-> (dc/decimal 11)
    (dc/div 2)
    (dc/mul 9)
    (dc/floor)) ;; #decimal 49

Almost all of the methods of JavaScript’s Math object are also replicated.

(dc/sqrt "6.98372465832e+9823")     ;; #decimal 8.3568682281821340204e+4911
(dc/pow 2 0.0979843)                ;; #decimal 1.0702770511687781839

There are NaN? and finite? methods, as NaN and Infinity are valid Decimal values,

(dc/NaN? (dc/decimal js/NaN)) ;; true
(dc/finite? (dc/decimal js/Infinity)) ;; false

and a to-fraction method with an optional maximum denominator argument

(let [pi (dc/div (dc/decimal 355) 113)]
  (dc/to-fraction pi)         ;; [#decimal 31415929203539823009 #decimal 10000000000000000000]
  (dc/to-fraction pi 1000))   ;; [#decimal 355 #decimal 113]

All calculations are rounded according to the number of significant digits and rounding mode specified by the precision and rounding properties of the Decimal constructor.

Multiple Decimal constructors can be created, each with their own independent configuration which applies to all Decimal numbers created from it.

;; Set the precision and rounding of the default Decimal constructor
(dc/config! {:precision 5 :rounding :round-half-up})

;; Create another Decimal constructor, optionally passing in a configuration object
(let [decimal10 (dc/config {:precision 10 :rounding :round-down})
      x (dc/decimal 5)
      y (decimal10 5)]
  (dc/div x 3)     ;; #decimal 1.6667
  (dc/div y 3))    ;; #decimal 1.666666666

Developers Guide

Philosophy

Five most important rules:

  • Beautiful is better than ugly.

  • Explicit is better than implicit.

  • Simple is better than complex.

  • Complex is better than complicated.

  • Readability counts.

All contributions to decimal should keep these important rules in mind.

Contributing

Unlike Clojure and other Clojure contributed libraries decimal does not have many restrictions for contributions. Just open an issue or pull request.

Source Code

decimal is open source and can be found on github.

You can clone the public repository with this command:

git clone https://github.com/funcool/decimal

Run tests

For running tests just execute this:

ClojureScript
./scrpts/build
node ./out/tests.js

License

decimal is licensed under BSD (2-Clause) license:

Copyright (c) 2015-2017 Andrey Antukh <niwi@niwi.nz>
Copyright (c) 2017-2017 Jesús Espino <jespinog@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.