From dc036179b79bcb5cc04ce4655b629661189f70cf Mon Sep 17 00:00:00 2001 From: Dipak Sarkar Date: Thu, 31 Mar 2022 21:31:24 +0530 Subject: [PATCH] fix nan issue for reverse fill --- src/number-format.js | 2 +- tests/unit/number-format.reverse-fill.spec.js | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/number-format.js b/src/number-format.js index 4cdec5f..7476cd8 100644 --- a/src/number-format.js +++ b/src/number-format.js @@ -38,7 +38,7 @@ export default function NumberFormat(config = options) { function toFixed(numbers, precision) { // eslint-disable-next-line no-restricted-properties var exp = Math.pow(10, precision); - var float = parseFloat(numbers) / exp; + var float = parseFloat(numbers) / exp || 0; return float.toFixed(fixed(precision)); } diff --git a/tests/unit/number-format.reverse-fill.spec.js b/tests/unit/number-format.reverse-fill.spec.js index 8895784..acaab12 100644 --- a/tests/unit/number-format.reverse-fill.spec.js +++ b/tests/unit/number-format.reverse-fill.spec.js @@ -3,13 +3,21 @@ import NumberFormat from "../../src/number-format"; describe("when enabled reverse fill", () => { const numberFormat = new NumberFormat({ reverseFill: true, + prefix: "$", }); it("should return as follows", () => { - expect(numberFormat.format("sdfgasd55468.546")).toEqual("554,685.46"); - expect(numberFormat.format("sdfgasd55468.546")).toEqual("554,685.46"); - expect(numberFormat.format("sdfgasd55468.546-")).toEqual("-554,685.46"); - expect(numberFormat.format("-1234.6512")).toEqual("-123,465.12"); - expect(numberFormat.format("0")).toEqual("0.00"); + expect(numberFormat.format("sdfgasd55468.546")).toEqual("$554,685.46"); + expect(numberFormat.format("sdfgasd55468.546")).toEqual("$554,685.46"); + expect(numberFormat.format("sdfgasd55468.546-")).toEqual("-$554,685.46"); + expect(numberFormat.format("-1234.6512")).toEqual("-$123,465.12"); + expect(numberFormat.format("0")).toEqual("$0.00"); + }); + it("should return as follows", () => { + expect(numberFormat.format("sdfgasd55468.546")).toEqual("$554,685.46"); + expect(numberFormat.format("sdfgasd55468.546")).toEqual("$554,685.46"); + expect(numberFormat.format("sdfgasd55468.546-")).toEqual("-$554,685.46"); + expect(numberFormat.format("-1234.6512")).toEqual("-$123,465.12"); + expect(numberFormat.format(0)).toEqual("$0.00"); }); it("should return as follows", () => { expect(numberFormat.unformat("sdfgasd55468.546")).toEqual("554685.46");