Skip to content

Commit d219714

Browse files
authored
fix: adds bignumberish type to amount utilities, stop addNumberDecimals returning scientific notation (#2)
1 parent 390562a commit d219714

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

src/utilities/amounts.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { BigNumber } from "./bignumber.js";
1+
import { BigNumber, BigNumberish } from "./bignumber.js";
22
import { Decimal } from "decimal.js";
33

44
/**
55
* Extract decimals from a number - converts human-readable amount to base units
6-
* @param num - The amount as a number (e.g., 1.5 ZNN)
6+
* @param num - The amount as BigNumberish (e.g., 1.5, "1.5", bigint)
77
* @param decimals - Number of decimals (e.g., 8 for ZNN)
88
* @returns BigNumber representing the amount in base units
99
* @example extractNumberDecimals(1.5, 8) => BigNumber(150000000)
1010
*/
11-
export function extractNumberDecimals(num: number, decimals: number): BigNumber {
12-
const decimal = new Decimal(num);
11+
export function extractNumberDecimals(num: BigNumberish, decimals: number): BigNumber {
12+
const decimal = new Decimal(num.toString());
1313
const scalingFactor = new Decimal(10).pow(decimals);
1414
const result = decimal.times(scalingFactor);
1515
// Truncate to remove any fractional part (matching Dart SDK behavior)
@@ -23,10 +23,10 @@ export function extractNumberDecimals(num: number, decimals: number): BigNumber
2323
* @returns String representing the human-readable amount
2424
* @example addNumberDecimals(150000000, 8) => "1.5"
2525
*/
26-
export function addNumberDecimals(num: number, decimals: number): string {
27-
const decimal = new Decimal(num);
26+
export function addNumberDecimals(num: BigNumberish, decimals: number): string {
27+
const decimal = new Decimal(num.toString());
2828
const scalingFactor = new Decimal(10).pow(decimals);
2929
const result = decimal.dividedBy(scalingFactor);
30-
return result.toString();
30+
return result.toFixed();
3131
}
3232

test/utilities/amount.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe("Amount", () => {
66
it("should multiply number by 10^decimals and return BigNumber", () => {
77
expect(extractNumberDecimals(5, 2).toString()).to.equal("500");
88
expect(extractNumberDecimals(1.23, 3).toString()).to.equal("1230");
9+
expect(extractNumberDecimals(0.00000001, 8).toString()).to.equal("1");
910
expect(extractNumberDecimals(0, 5).toString()).to.equal("0");
1011
});
1112

@@ -34,6 +35,7 @@ describe("Amount", () => {
3435
it("should divide number by 10^decimals and return string", () => {
3536
expect(addNumberDecimals(500, 2)).to.equal("5");
3637
expect(addNumberDecimals(1230, 3)).to.equal("1.23");
38+
expect(addNumberDecimals(1, 8)).to.equal("0.00000001");
3739
expect(addNumberDecimals(0, 5)).to.equal("0");
3840
});
3941

0 commit comments

Comments
 (0)