From 91dfae9ef674fb0ebeccdd3bdf4a4be33da512f1 Mon Sep 17 00:00:00 2001 From: RgnDunes Date: Mon, 16 Dec 2024 15:24:29 +0530 Subject: [PATCH 1/4] fix: fix floating point issue in convertToMinorUnit --- .../i18nify-js/src/modules/currency/convertToMinorUnit.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts index 39765438..a6c00960 100644 --- a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts +++ b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts @@ -31,7 +31,9 @@ const convertToMinorUnit = ( const minorUnitMultiplier = Math.pow(10, Number(currencyInfo.minor_unit)) || 100; - const lowerCurrencyValue = amount * minorUnitMultiplier; + const lowerCurrencyValue = parseFloat( + (amount * minorUnitMultiplier).toFixed(Number(currencyInfo.minor_unit)), + ); return lowerCurrencyValue; }; From 71f4b5c2c5e51e23f67d88d751c55732b1c95c23 Mon Sep 17 00:00:00 2001 From: RgnDunes Date: Mon, 16 Dec 2024 16:31:06 +0530 Subject: [PATCH 2/4] add UT --- .../modules/currency/__tests__/convertToMinorUnit.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts b/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts index 7700b084..8a76a803 100644 --- a/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts +++ b/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts @@ -30,4 +30,9 @@ describe('currency - convertToMinorUnit', () => { `Error: The provided currency code is either empty or not supported. The received value was : ${unsupportedCurrencyCode}. Please ensure you pass a valid currency code.`, ); }); + + it('should correctly convert amount to minor unit for INR', () => { + const result = convertToMinorUnit(4.14, { currency: 'INR' }); + expect(result).toBe(414); + }); }); From e76fda627eac3050acf2ca26d80fd7ca0c53cd39 Mon Sep 17 00:00:00 2001 From: RgnDunes Date: Mon, 16 Dec 2024 16:50:09 +0530 Subject: [PATCH 3/4] resolve review comments --- packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts index a6c00960..fce46eee 100644 --- a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts +++ b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts @@ -32,7 +32,7 @@ const convertToMinorUnit = ( Math.pow(10, Number(currencyInfo.minor_unit)) || 100; const lowerCurrencyValue = parseFloat( - (amount * minorUnitMultiplier).toFixed(Number(currencyInfo.minor_unit)), + (amount * minorUnitMultiplier).toFixed(0), ); return lowerCurrencyValue; }; From bdbf9dd71452e497e80abaa470c2c86585f5778a Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Mon, 16 Dec 2024 18:47:28 +0530 Subject: [PATCH 4/4] Create mean-pumas-deny.md --- .changeset/mean-pumas-deny.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/mean-pumas-deny.md diff --git a/.changeset/mean-pumas-deny.md b/.changeset/mean-pumas-deny.md new file mode 100644 index 00000000..1e52020e --- /dev/null +++ b/.changeset/mean-pumas-deny.md @@ -0,0 +1,5 @@ +--- +"@razorpay/i18nify-js": patch +--- + +fix floating point issue in convertToMinorUnit