SAO is a library that provides (maybe) strict arithmetic for JavaScript.
At first, download sao.js. Then, put this line in your html.
<script src="sao.js"></script>
Or,
<script src="https://raw.github.com/matsukei/sao.js/master/sao.js"></script>
That's all.
var result = sao.add(100, 200, '300'); // => 600 <Object(goog.math.Long)>
But result
is not a Number. If you want to get a Number, need to call sao#finalize
:
sao.finalize(result); // => 600
var result = sao.sub(20.2, 20); // => 0.2 <Object(goog.math.Long)>
sao.finalize(result); // => 0.2 <Number>
var result = sao.mul(0.01, 10); // => 0.1 <Object(goog.math.Long)>
sao.finalize(result); // => 0.1 <Number>
var result = sao.div(9, '3'); // => 3 <Object(goog.math.Long)>
sao.finalize(result); // 3 <Number>
sao.round(1.456, 1); // => 1.5 <Number (Not an Object!)>
sao.round(1.456); // => 2 <Number (Not an Object!)>
sao.calc(10, '*', 2, '+', 5, '/', 2); // => 12.5 <Number>
This is the same as the following:
sao.div(sao.add(sao.mul(10, 2), 5), 2);
See sao/base_test.html (UnitTest).
- Clone repository (
git clone git://github.com/matsukei/sao
) cd sao/
rake test
or openall_tests.html
in your browser(Safari or Firefox)- Press
<T>
key to run
Requirements:
- Ruby (
>= 1.9
) - Python
- Java
Compile:
- Clone repository (
git clone git://github.com/matsukei/sao
) cd sao/
rake compile
- Create or overwrite
sao.js
- Fork it
- Create your feature branch (
git checkout -b new-feature
) - Commit your changes (
git commit -am
add some new feature``) - Push to the branch (
git push origin new-feature
) - Create new Pull Request
See LICENSE file.