@@ -532,8 +532,9 @@ export const fromWei = (number: Numbers, unit: EtherUnits): string => {
532532 if ( fraction === '' ) {
533533 return integer ;
534534 }
535+ const updatedValue = `${ integer } .${ fraction } ` ;
535536
536- return ` ${ integer } . ${ fraction } ` ;
537+ return updatedValue . slice ( 0 , integer . length + numberOfZerosInDenomination + 1 ) ;
537538} ;
538539
539540/**
@@ -559,50 +560,50 @@ export const toWei = (number: Numbers, unit: EtherUnits): string => {
559560 throw new InvalidUnitError ( unit ) ;
560561 }
561562 let parsedNumber = number ;
562- if ( typeof parsedNumber === 'number' ) {
563- if ( parsedNumber < 1e-15 ) {
564- console . warn ( PrecisionLossWarning )
563+ if ( typeof parsedNumber === 'number' ) {
564+ if ( parsedNumber < 1e-15 ) {
565+ console . warn ( PrecisionLossWarning ) ;
565566 }
566- if ( parsedNumber > 1e+20 ) {
567- console . warn ( PrecisionLossWarning )
567+ if ( parsedNumber > 1e20 ) {
568+ console . warn ( PrecisionLossWarning ) ;
568569
569- parsedNumber = BigInt ( parsedNumber ) ;
570+ parsedNumber = BigInt ( parsedNumber ) ;
570571 } else {
571572 // in case there is a decimal point, we need to convert it to string
572- parsedNumber = parsedNumber . toLocaleString ( 'fullwide' , { useGrouping : false , maximumFractionDigits : 20 } )
573+ parsedNumber = parsedNumber . toLocaleString ( 'fullwide' , {
574+ useGrouping : false ,
575+ maximumFractionDigits : 20 ,
576+ } ) ;
573577 }
574578 }
575-
579+
576580 // if value is decimal e.g. 24.56 extract `integer` and `fraction` part
577581 // to avoid `fraction` to be null use `concat` with empty string
578582 const [ integer , fraction ] = String (
579- typeof parsedNumber === 'string' && ! isHexStrict ( parsedNumber ) ? parsedNumber : toNumber ( parsedNumber ) ,
583+ typeof parsedNumber === 'string' && ! isHexStrict ( parsedNumber )
584+ ? parsedNumber
585+ : toNumber ( parsedNumber ) ,
580586 )
581587 . split ( '.' )
582588 . concat ( '' ) ;
583589
584590 // join the value removing `.` from
585591 // 24.56 -> 2456
586-
592+
587593 const value = BigInt ( `${ integer } ${ fraction } ` ) ;
588594
589595 // multiply value with denomination
590596 // 2456 * 1000000 -> 2456000000
591597 const updatedValue = value * denomination ;
592598
593- // count number of zeros in denomination
594- const numberOfZerosInDenomination = denomination . toString ( ) . length - 1 ;
595-
596- // check which either `fraction` or `denomination` have lower number of zeros
597- const decimals = Math . min ( fraction . length , numberOfZerosInDenomination ) ;
598-
599+ // check if whole number was passed in
600+ const decimals = fraction . length ;
599601 if ( decimals === 0 ) {
600602 return updatedValue . toString ( ) ;
601603 }
602604
603- // Add zeros to make length equal to required decimal points
604- // If string is larger than decimal points required then remove last zeros
605- return updatedValue . toString ( ) . padStart ( decimals , '0' ) . slice ( 0 , - decimals ) ;
605+ // trim the value to remove extra zeros
606+ return updatedValue . toString ( ) . slice ( 0 , - decimals ) ;
606607} ;
607608
608609/**
0 commit comments