Skip to content

Commit

Permalink
fix nan issue for reverse fill
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipak Sarkar committed Mar 31, 2022
1 parent 2d1cafa commit dc03617
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/number-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
18 changes: 13 additions & 5 deletions tests/unit/number-format.reverse-fill.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit dc03617

Please sign in to comment.