import BigNumber from 'bignumber.js' ; export const splitNumberByStep = ( num , step = 3 , symbol = ',' , forceInt = false ) => { let [ int , float ] = ( num + '' ) . split ( '.' ) ; const reg = new RegExp ( `(\\d)(?=(\\d{${ step }})+(?!\\d))` , 'g' ) ; int = int . replace ( reg , `$1${ symbol }` ) ; if ( Number ( num ) > 1000000 || forceInt ) { float = '' ; } if ( float ) { return `${ int }.${ float }` ; } return int ; } ; export const formatTokenAmount = ( amount , decimals = 4 ) => { if ( ! amount ) return '0' ; const bn = new BigNumber ( amount ) ; const str = bn . toFixed ( ) ; const split = str . split ( '.' ) ; if ( ! split [ 1 ] || split [ 1 ] . length < decimals ) { return splitNumberByStep ( bn . toFixed ( ) ) ; } return splitNumberByStep ( bn . toFixed ( decimals ) ) ; } ; export const numberWithCommasIsLtOne = ( x ? , precision ? ) => { if ( x === undefined || x === null ) { return '-' ; } if ( x === 0 ) return '0' ; if ( x < 0.00005 ) { return '< 0.0001' ; } precision = x < 1 ? 4 : precision ?? 2 ; const parts = Number ( x ) . toFixed ( precision ) . split ( '.' ) ; parts [ 0 ] = parts [ 0 ] . replace ( /\B(?=(\d{3})+(?!\d))/g , ',' ) ; return parts . join ( '.' ) ; } ;