diff --git a/app/components/Views/SendFlow/Amount/index.js b/app/components/Views/SendFlow/Amount/index.js index 3cc4246b3f3..89c0a70ec8a 100644 --- a/app/components/Views/SendFlow/Amount/index.js +++ b/app/components/Views/SendFlow/Amount/index.js @@ -44,6 +44,7 @@ import { fromTokenMinimalUnitString, toHexadecimal, hexToBN, + formatValueToMatchTokenDecimals, } from '../../../../util/number'; import { getTicker, @@ -610,6 +611,8 @@ class Amount extends PureComponent { if (value && value.includes(',')) { value = inputValue.replace(',', '.'); } + + value = formatValueToMatchTokenDecimals(value, selectedAsset.decimals); if ( !selectedAsset.tokenId && this.validateAmount(value, internalPrimaryCurrencyIsCrypto) diff --git a/app/util/number/index.js b/app/util/number/index.js index 26b7eb365fc..b1ddb6bee9b 100644 --- a/app/util/number/index.js +++ b/app/util/number/index.js @@ -840,3 +840,17 @@ export const isZeroValue = (value) => { } return value === '0x0' || (isBN(value) && value.isZero()) || isZero(value); }; + +export const formatValueToMatchTokenDecimals = (value, decimal) => { + if (value === null || value === undefined) { + return value; + } + const decimalIndex = value.indexOf('.'); + if (decimalIndex !== -1) { + const fractionalLength = value.substring(decimalIndex + 1).length; + if (fractionalLength > decimal) { + value = parseFloat(value).toFixed(decimal); + } + } + return value; +}; diff --git a/app/util/number/index.test.ts b/app/util/number/index.test.ts index ada0dafb678..103cfb10649 100644 --- a/app/util/number/index.test.ts +++ b/app/util/number/index.test.ts @@ -11,6 +11,7 @@ import { fastSplit, fiatNumberToTokenMinimalUnit, fiatNumberToWei, + formatValueToMatchTokenDecimals, fromTokenMinimalUnit, fromTokenMinimalUnitString, fromWei, @@ -819,3 +820,27 @@ describe('Number utils :: isZeroValue', () => { expect(isZeroValue(toBN('0'))).toBe(true); }); }); + +describe('Number utils :: formatValueToMatchTokenDecimals', () => { + it('should return a formatted value if the submitted decimals is 0', () => { + expect(formatValueToMatchTokenDecimals('1.0', 0)).toBe('1'); + }); + it('should return the value if value is null', () => { + expect(formatValueToMatchTokenDecimals(null, 18)).toBe(null); + }); + it('should return the value if the decimal is undefined', () => { + expect(formatValueToMatchTokenDecimals('1', undefined)).toBe('1'); + }); + it('should return a formatted value if the decimal is null', () => { + expect(formatValueToMatchTokenDecimals('1', null)).toBe('1'); + }); + it('should return the value if the decimal is not a number', () => { + expect(formatValueToMatchTokenDecimals('1', 'a')).toBe('1'); + }); + it('should return the value if the value decimal is equal to or less than the submitted decimal', () => { + expect(formatValueToMatchTokenDecimals('1.2348', 4)).toBe('1.2348'); + }); + it('should return a formatted value if the value decimal is greater than the submitted decimal', () => { + expect(formatValueToMatchTokenDecimals('1.234567', 4)).toBe('1.2346'); + }); +});