Fix: Precision got messed up
This commit is contained in:
+16
-33
@@ -145,43 +145,26 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function dec (num, deci) {
|
function dec (num, deci) {
|
||||||
let amount;
|
const int = parseInt (num, 10);
|
||||||
let result;
|
const flo = parseFloat ((num % 1).toFixed(deci));
|
||||||
let missing;
|
const len = (String (flo).split ('.')[1] || '').length;
|
||||||
let i;
|
const diff = deci - len;
|
||||||
|
let res = String (int + flo);
|
||||||
|
|
||||||
// rounding
|
if (!flo && diff) {
|
||||||
if (deci) {
|
res += '.';
|
||||||
amount = Math.pow (10, deci || 1);
|
for (let i = 0; i < diff; i++) {
|
||||||
result = Math.round (num * amount) / amount;
|
res += '0';
|
||||||
}
|
|
||||||
else {
|
|
||||||
result = Math.round (num);
|
|
||||||
}
|
|
||||||
|
|
||||||
// force trailing zeros
|
|
||||||
result = String (result);
|
|
||||||
|
|
||||||
if (!deci) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.indexOf ('.') > -1) {
|
|
||||||
missing = deci;
|
|
||||||
result += '.';
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
missing = deci - result.split ('.')[1].length;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (missing) {
|
|
||||||
for (i = missing; i > 0; i--) {
|
|
||||||
result += '0';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// done
|
if (flo && diff > 0) {
|
||||||
return result;
|
for (let i = 0; i < diff; i++) {
|
||||||
|
res += '0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user