diff options
-rw-r--r-- | lib/decmath.js | 40 |
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") { |