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 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); + }); }); diff --git a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts index 39765438..fce46eee 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(0), + ); return lowerCurrencyValue; };