Skip to content

Commit

Permalink
Fixes rounding for negative number
Browse files Browse the repository at this point in the history
  • Loading branch information
uzyn committed Jun 24, 2020
1 parent 44fd767 commit 03ad45c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
3 changes: 2 additions & 1 deletion mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,13 @@ Deno.test("valueOf()", () => {

Deno.test("toFixed()", () => {
assertEquals(new BigDenary("12345678.1468").toFixed(), "12345678.1468");
assertEquals(new BigDenary("12345678.1468").toFixed(2), "12345678.14");
assertEquals(new BigDenary("12345678.1468").toFixed(2), "12345678.15");
assertEquals(new BigDenary("12345678.1468").toFixed(5), "12345678.14680");
assertEquals(new BigDenary("12").toFixed(5), "12.00000");
assertEquals(new BigDenary("0.1234").toFixed(5), "0.12340");
assertEquals(new BigDenary("-0.1234").toFixed(2), "-0.12");
assertEquals(new BigDenary("-0.1234").toFixed(5), "-0.12340");
assertEquals(new BigDenary("-0.1234898").toFixed(5), "-0.12349");
});

Deno.test("trimTrailingZeros()", () => {
Expand Down
36 changes: 17 additions & 19 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { countTrailingZeros, extractExp, getDecimals } from "./util.ts";
import {
bigIntAbs,
countTrailingZeros,
extractExp,
getDecimals,
} from "./util.ts";

export interface BigDenaryRaw {
base: bigint;
Expand Down Expand Up @@ -92,23 +97,12 @@ export class BigDenary {
}

toFixed(digits?: number): string {
const str = this.toString();
if (!digits || digits < 0) {
return str;
if (!digits) {
return this.toString();
}

const decimals = getDecimals(str);

if (digits === decimals) {
return str;
} else if (digits > decimals) {
const addZeros = _strOfZeros(digits - decimals);
if (this._decimals === 0) {
return `${str}.${addZeros}`;
}
return `${str}${addZeros}`;
}
return str.substr(0, str.length - (decimals - digits));
const temp = new BigDenary(this);
temp.scaleDecimalsTo(digits);
return temp.toString();
}

get decimals(): number {
Expand All @@ -127,8 +121,12 @@ export class BigDenary {
const multiplier = BigDenary.getDecimalMultiplier(adjust);
const remainder = this.base % multiplier;
this.base = this.base / multiplier;
if (remainder * 2n >= multiplier) {
this.base += 1n;
if (bigIntAbs(remainder * 2n) >= multiplier) {
if (this.base >= 0) {
this.base += 1n;
} else {
this.base -= 1n;
}
}
}
this._decimals = _decimals;
Expand Down
14 changes: 13 additions & 1 deletion util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import {
assertEquals,
assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
import { countTrailingZeros, extractExp, getDecimals } from "./util.ts";
import {
bigIntAbs,
countTrailingZeros,
extractExp,
getDecimals,
} from "./util.ts";

Deno.test("getDecimals() with string input", () => {
assertEquals(getDecimals("0.52135"), 5);
Expand Down Expand Up @@ -41,3 +46,10 @@ Deno.test("countTrailingZeros()", () => {
assertEquals(countTrailingZeros(-9846515000000000000000n, 100), 15);
assertEquals(countTrailingZeros(-9846515000000000000000n, 10), 10);
});

Deno.test("bigIntAbs()", () => {
assertEquals(bigIntAbs(0n), 0n);
assertEquals(bigIntAbs(-0n), 0n);
assertEquals(bigIntAbs(698465465n), 698465465n);
assertEquals(bigIntAbs(-98498465n), 98498465n);
});
7 changes: 7 additions & 0 deletions util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export function bigIntAbs(n: bigint): bigint {
if (n >= 0n) {
return n;
}
return n * -1n;
}

export function getDecimals(n: number | string): number {
if (isNaN(n as any)) {
throw new Error("InvalidNumber");
Expand Down

0 comments on commit 03ad45c

Please sign in to comment.