File tree Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import usePause from "@/hooks/use-pause";
10
10
import { IToken } from "@/types" ;
11
11
import {
12
12
formatNumber ,
13
- formatNumberSubscript ,
13
+ formatNumberSubscriptSmart ,
14
14
fromNow ,
15
15
LAMPORTS_PER_SOL ,
16
16
resizeImage ,
@@ -162,8 +162,7 @@ export default function SwapsTable({ token }: { token: IToken }) {
162
162
className = "size-2.5 rounded-full"
163
163
/>
164
164
< span className = "text-sm" >
165
- { formatNumberSubscript ( Number ( solana ) , 2 ) }
166
- { /* {formatNumber(solana, true, true)} */ }
165
+ { formatNumberSubscriptSmart ( Number ( solana ) , 2 ) }
167
166
</ span >
168
167
{ usdValue ? (
169
168
< span className = "text-autofun-text-secondary text-xs" >
Original file line number Diff line number Diff line change @@ -138,6 +138,43 @@ export const formatNumberSubscript = (
138
138
}
139
139
} ;
140
140
141
+ export const formatNumberSubscriptSmart = (
142
+ num : number ,
143
+ decimals : number = 4 ,
144
+ ) : string => {
145
+ if ( num === 0 ) return "0" ;
146
+ let sign = "" ;
147
+ if ( num < 0 ) {
148
+ sign = "-" ;
149
+ num = Math . abs ( num ) ;
150
+ }
151
+
152
+ if ( num >= 1 ) {
153
+ return sign + num . toFixed ( decimals ) . toString ( ) ;
154
+ }
155
+
156
+ const expStr = num . toExponential ( ) ;
157
+ const [ mantissa , exponentStr ] = expStr . split ( "e" ) ;
158
+ const exponent = parseInt ( exponentStr , 10 ) ;
159
+ let totalZeros = - exponent - 1 ;
160
+
161
+ if ( totalZeros < 0 ) {
162
+ totalZeros = 0 ;
163
+ }
164
+
165
+ if ( totalZeros >= decimals ) {
166
+ const mantissaDigits = mantissa . replace ( "." , "" ) . slice ( 0 , decimals + 1 ) ;
167
+ return sign + "0." + toSubscript ( totalZeros ) + mantissaDigits ;
168
+ } else {
169
+ const roundedMantissa =
170
+ Math . ceil ( Number ( mantissa ) * 10 ** decimals ) / 10 ** decimals ;
171
+ const roundedString = roundedMantissa . toFixed ( decimals ) ;
172
+ const mantissaDigits = roundedString . replace ( "." , "" ) . slice ( 0 , decimals ) ;
173
+
174
+ return sign + "0." + "0" . repeat ( totalZeros ) + mantissaDigits ;
175
+ }
176
+ } ;
177
+
141
178
export const isFromDomain = ( url : string , domain : string ) : boolean => {
142
179
// if url does not have http or https, add it
143
180
if ( ! url . startsWith ( "http" ) && ! url . startsWith ( "https" ) ) {
You can’t perform that action at this time.
0 commit comments