@@ -12,15 +12,17 @@ export enum CoinType {
12
12
}
13
13
14
14
/**
15
- * @param options.truncate Whether to truncate the address with K, M, B and T units, defaults to `true`. Disabled when `options.decimals` is `null `
15
+ * @param options.roundingMode The rounding mode to use, defaults to `BigNumber.ROUND_DOWN `
16
16
* @param options.decimals How many decimals to display, `null` disables rounding, defaults to `3`
17
+ * @param options.truncate Whether to truncate the address with K, M, B and T units, defaults to `true`. Disabled when `options.decimals` is `null`
17
18
* @param options.padZeros Whether add trailing zeros to the end of the string, defaults to `false`
18
19
* @param options.addUnit Whether to display the unit, defaults to `true`
19
20
* @param options.prefix The prefix to prepend to the formatted string
20
21
*/
21
22
export interface FilecoinFormatOptions {
22
- truncate ?: boolean
23
+ roundingMode ?: BigNumber . RoundingMode
23
24
decimals ?: number | null
25
+ truncate ?: boolean
24
26
padZeros ?: boolean
25
27
addUnit ?: boolean
26
28
prefix ?: string
@@ -206,15 +208,17 @@ export class FilecoinNumber extends BigNumber {
206
208
* Expresses this FilecoinNumber as a balance string
207
209
*/
208
210
formatBalance ( options ?: FilecoinFormatOptions ) : string {
211
+ // Round down by default to avoid showing a higher balance
212
+ const roundingMode = options ?. roundingMode ?? BigNumber . ROUND_DOWN
213
+ const roundValue = options ?. decimals !== null
214
+ const decimals = options ?. decimals ?? 2
209
215
const truncate = options ?. truncate ?? true
210
- const round = options ?. decimals !== null
211
- const decimals = options ?. decimals ?? 3
212
216
const padZeros = options ?. padZeros ?? false
213
217
const addUnit = options ?. addUnit ?? true
214
218
215
219
const toFormat = ( value : BigNumber , format : BigNumber . Format ) : string =>
216
220
padZeros
217
- ? value . toFormat ( decimals , BigNumber . ROUND_DOWN , format )
221
+ ? value . toFormat ( roundValue ? decimals : 18 , roundingMode , format )
218
222
: value . toFormat ( format )
219
223
220
224
// Create format configuration
@@ -228,10 +232,10 @@ export class FilecoinNumber extends BigNumber {
228
232
229
233
// When not rounding, it doesn't make sense to truncate either.
230
234
// Return the original value when it's zero or when not rounding.
231
- if ( this . isZero ( ) || ! round ) return toFormat ( this , format )
235
+ if ( this . isZero ( ) || ! roundValue ) return toFormat ( this , format )
232
236
233
- // Round down by default to avoid showing higher balance
234
- const rounded = this . dp ( decimals , BigNumber . ROUND_DOWN )
237
+ // Round value to the specified decimals
238
+ const rounded = this . dp ( decimals , roundingMode )
235
239
236
240
// Value is zero after rounding
237
241
if ( rounded . isZero ( ) ) {
@@ -259,11 +263,14 @@ export class FilecoinNumber extends BigNumber {
259
263
const units = [ 'K' , 'M' , 'B' , 'T' ]
260
264
for ( const unit of units ) {
261
265
const unitRaw = rounded . dividedBy ( Math . pow ( 1000 , ++ power ) )
262
- const unitVal = unitRaw . dp ( 1 , BigNumber . ROUND_DOWN )
266
+ const unitVal = unitRaw . dp ( 1 , roundingMode )
263
267
const isLt1K = unitVal . isGreaterThan ( - 1000 ) && unitVal . isLessThan ( 1000 )
264
268
if ( isLt1K || unit === 'T' ) {
265
269
const suffix = `${ unit } ${ format . suffix } `
266
- return unitVal . toFormat ( { ...format , suffix } )
270
+ const unitFmt = { ...format , suffix }
271
+ return padZeros
272
+ ? unitVal . toFormat ( 1 , roundingMode , unitFmt )
273
+ : unitVal . toFormat ( unitFmt )
267
274
}
268
275
}
269
276
0 commit comments