summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2017-01-07 04:33:10 (EST)
committer P. J. McDermott <pj@pehjota.net>2017-01-07 04:33:10 (EST)
commit2dc86b808a2283a17b9be8910ffd067ff118324e (patch)
tree5373a82f6fdff5fa4c20821870920d6b9b9f4127
parent3197a5b4bc521f9ab4c73c91b9bd750d06e8f5e1 (diff)
downloaddecmath.js-2dc86b808a2283a17b9be8910ffd067ff118324e.zip
decmath.js-2dc86b808a2283a17b9be8910ffd067ff118324e.tar.gz
decmath.js-2dc86b808a2283a17b9be8910ffd067ff118324e.tar.bz2
drhtz(), drhfz(): Implement
-rw-r--r--lib/decmath.js40
1 files changed, 38 insertions, 2 deletions
diff --git a/lib/decmath.js b/lib/decmath.js
index 45f7cb4..4d2da54 100644
--- a/lib/decmath.js
+++ b/lib/decmath.js
@@ -72,12 +72,48 @@ function dmul(a, b)
function drhtz(a, p)
{
- /* TODO: Implement */
+ a = a.toString().split(".", 2);
+ if (a.length == 1) return null;
+ var ad = a[1].substr(p);
+ a[1] = a[1].substr(0, p);
+ //console.log("ai: " + a[0] + ", af: " + a[1] + ", ad: " + ad);
+ var as = a.join("");
+ //console.log("as: " + as);
+ if (as.charAt(0) != "-") {
+ if (parseInt(ad, 10) > 5 * Math.pow(10, ad.length - 1)) {
+ as = (parseInt(as) + 1).toString();
+ }
+ } else {
+ if (parseInt(ad, 10) > 5 * Math.pow(10, ad.length - 1)) {
+ as = (parseInt(as) - 1).toString();
+ }
+ }
+ var pad = p - as.length;
+ if (pad > 0) as = (Array(pad + 1).join("0") + as).toString();
+ return as.substr(0, as.length - p) + "." + as.substr(as.length - p);
}
function drhfz(a, p)
{
- /* TODO: Implement */
+ a = a.toString().split(".", 2);
+ if (a.length == 1) return null;
+ var ad = a[1].substr(p);
+ a[1] = a[1].substr(0, p);
+ //console.log("ai: " + a[0] + ", af: " + a[1] + ", ad: " + ad);
+ var as = a.join("");
+ //console.log("as: " + as);
+ if (as.charAt(0) != "-") {
+ if (parseInt(ad, 10) >= 5 * Math.pow(10, ad.length - 1)) {
+ as = (parseInt(as) + 1).toString();
+ }
+ } else {
+ if (parseInt(ad, 10) >= 5 * Math.pow(10, ad.length - 1)) {
+ as = (parseInt(as) - 1).toString();
+ }
+ }
+ var pad = p - as.length;
+ if (pad > 0) as = (Array(pad + 1).join("0") + as).toString();
+ return as.substr(0, as.length - p) + "." + as.substr(as.length - p);
}
if (typeof global != "undefined") {