@@ -532,8 +532,9 @@ export const fromWei = (number: Numbers, unit: EtherUnits): string => {
532
532
if ( fraction === '' ) {
533
533
return integer ;
534
534
}
535
+ const updatedValue = `${ integer } .${ fraction } ` ;
535
536
536
- return ` ${ integer } . ${ fraction } ` ;
537
+ return updatedValue . slice ( 0 , integer . length + numberOfZerosInDenomination + 1 ) ;
537
538
} ;
538
539
539
540
/**
@@ -559,50 +560,50 @@ export const toWei = (number: Numbers, unit: EtherUnits): string => {
559
560
throw new InvalidUnitError ( unit ) ;
560
561
}
561
562
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 ) ;
565
566
}
566
- if ( parsedNumber > 1e+20 ) {
567
- console . warn ( PrecisionLossWarning )
567
+ if ( parsedNumber > 1e20 ) {
568
+ console . warn ( PrecisionLossWarning ) ;
568
569
569
- parsedNumber = BigInt ( parsedNumber ) ;
570
+ parsedNumber = BigInt ( parsedNumber ) ;
570
571
} else {
571
572
// 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
+ } ) ;
573
577
}
574
578
}
575
-
579
+
576
580
// if value is decimal e.g. 24.56 extract `integer` and `fraction` part
577
581
// to avoid `fraction` to be null use `concat` with empty string
578
582
const [ integer , fraction ] = String (
579
- typeof parsedNumber === 'string' && ! isHexStrict ( parsedNumber ) ? parsedNumber : toNumber ( parsedNumber ) ,
583
+ typeof parsedNumber === 'string' && ! isHexStrict ( parsedNumber )
584
+ ? parsedNumber
585
+ : toNumber ( parsedNumber ) ,
580
586
)
581
587
. split ( '.' )
582
588
. concat ( '' ) ;
583
589
584
590
// join the value removing `.` from
585
591
// 24.56 -> 2456
586
-
592
+
587
593
const value = BigInt ( `${ integer } ${ fraction } ` ) ;
588
594
589
595
// multiply value with denomination
590
596
// 2456 * 1000000 -> 2456000000
591
597
const updatedValue = value * denomination ;
592
598
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 ;
599
601
if ( decimals === 0 ) {
600
602
return updatedValue . toString ( ) ;
601
603
}
602
604
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 ) ;
606
607
} ;
607
608
608
609
/**
0 commit comments