From: Chris Duncan Date: Tue, 8 Oct 2024 16:40:03 +0000 (-0700) Subject: Add test for converting small amounts of raw to fractional nano. Fix issue found... X-Git-Tag: v0.0.6~1 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=4ae6d4caa94f9734af6cd1d17f0ae6613a01cf39;p=libnemo.git Add test for converting small amounts of raw to fractional nano. Fix issue found with said test. --- diff --git a/src/lib/tools.ts b/src/lib/tools.ts index fb5c13d..22b0176 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -53,7 +53,7 @@ export async function convert (amount: bigint | string, inputUnit: string, outpu // convert to desired denomination if (UNITS[outputUnit] !== 0) { - f = i.slice(-UNITS[outputUnit]) + f + f = i.padStart(40, '0').slice(-UNITS[outputUnit]) + f i = i.slice(0, -UNITS[outputUnit]) } i = i.replace(/^0*/g, '') ?? '0' diff --git a/test/tools.test.mjs b/test/tools.test.mjs index d2c1b6f..08de50d 100644 --- a/test/tools.test.mjs +++ b/test/tools.test.mjs @@ -23,6 +23,11 @@ describe('unit conversion tests', async () => { assert.equal(result, '1') }) + it('should convert 1 raw to 10^-29 nano', async () => { + const result = await Tools.convert('1', 'RAW', 'NANO') + assert.equal(result, '.000000000000000000000000000001') + }) + it('should ignore leading and trailing zeros', async () => { const result = await Tools.convert('0011002200.0033004400', 'nano', 'nano') assert.equal(result, '11002200.00330044')