File tree Expand file tree Collapse file tree 2 files changed +17
-0
lines changed
packages/thirdweb/src/utils Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -199,6 +199,19 @@ describe("toUnits", () => {
199199 expect ( toUnits ( "69.59000000059" , 9 ) ) . toMatchInlineSnapshot ( "69590000001n" ) ;
200200 expect ( toUnits ( "69.59000002359" , 9 ) ) . toMatchInlineSnapshot ( "69590000024n" ) ;
201201 } ) ;
202+
203+ it ( "scientific notation" , ( ) => {
204+ // negative exponents
205+ expect ( toUnits ( "1e-18" , 18 ) ) . toMatchInlineSnapshot ( "1n" ) ;
206+ expect ( toUnits ( "1e-16" , 18 ) ) . toMatchInlineSnapshot ( "100n" ) ;
207+ expect ( toUnits ( "1.23456789012345e-7" , 18 ) ) . toMatchInlineSnapshot (
208+ "123456789012n" ,
209+ ) ;
210+ // positive exponents
211+ expect ( toUnits ( "1.234e3" , 18 ) ) . toMatchInlineSnapshot (
212+ "1234000000000000000000n" ,
213+ ) ;
214+ } ) ;
202215} ) ;
203216
204217describe ( "toWei" , ( ) => {
Original file line number Diff line number Diff line change @@ -73,6 +73,10 @@ export function toEther(wei: bigint) {
7373 * @utils
7474 */
7575export function toUnits ( tokens : string , decimals : number ) : bigint {
76+ if ( tokens . includes ( "e" ) ) {
77+ tokens = Number ( tokens ) . toFixed ( decimals ) ;
78+ }
79+
7680 let [ integerPart , fractionPart = "" ] = tokens . split ( "." ) as [ string , string ] ;
7781 const prefix = integerPart . startsWith ( "-" ) ? "-" : "" ;
7882 if ( prefix ) {
You can’t perform that action at this time.
0 commit comments