Skip to content

Commit 9d07f1e

Browse files
authoredSep 10, 2024··
Allow custom rounding for formatting FilecoinNumber (#323)
1 parent a3dd79c commit 9d07f1e

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed
 

‎packages/filecoin-number/src/FilecoinNumber.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ export enum CoinType {
1212
}
1313

1414
/**
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`
1616
* @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`
1718
* @param options.padZeros Whether add trailing zeros to the end of the string, defaults to `false`
1819
* @param options.addUnit Whether to display the unit, defaults to `true`
1920
* @param options.prefix The prefix to prepend to the formatted string
2021
*/
2122
export interface FilecoinFormatOptions {
22-
truncate?: boolean
23+
roundingMode?: BigNumber.RoundingMode
2324
decimals?: number | null
25+
truncate?: boolean
2426
padZeros?: boolean
2527
addUnit?: boolean
2628
prefix?: string
@@ -206,15 +208,17 @@ export class FilecoinNumber extends BigNumber {
206208
* Expresses this FilecoinNumber as a balance string
207209
*/
208210
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
209215
const truncate = options?.truncate ?? true
210-
const round = options?.decimals !== null
211-
const decimals = options?.decimals ?? 3
212216
const padZeros = options?.padZeros ?? false
213217
const addUnit = options?.addUnit ?? true
214218

215219
const toFormat = (value: BigNumber, format: BigNumber.Format): string =>
216220
padZeros
217-
? value.toFormat(decimals, BigNumber.ROUND_DOWN, format)
221+
? value.toFormat(roundValue ? decimals : 18, roundingMode, format)
218222
: value.toFormat(format)
219223

220224
// Create format configuration
@@ -228,10 +232,10 @@ export class FilecoinNumber extends BigNumber {
228232

229233
// When not rounding, it doesn't make sense to truncate either.
230234
// 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)
232236

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)
235239

236240
// Value is zero after rounding
237241
if (rounded.isZero()) {
@@ -259,11 +263,14 @@ export class FilecoinNumber extends BigNumber {
259263
const units = ['K', 'M', 'B', 'T']
260264
for (const unit of units) {
261265
const unitRaw = rounded.dividedBy(Math.pow(1000, ++power))
262-
const unitVal = unitRaw.dp(1, BigNumber.ROUND_DOWN)
266+
const unitVal = unitRaw.dp(1, roundingMode)
263267
const isLt1K = unitVal.isGreaterThan(-1000) && unitVal.isLessThan(1000)
264268
if (isLt1K || unit === 'T') {
265269
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)
267274
}
268275
}
269276

‎packages/filecoin-number/src/__tests__/number.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,13 @@ describe('FilecoinNumber', () => {
213213

214214
// Less then 1000
215215
expect(new FilecoinNumber('1.23456789', 'fil').formatBalance()).toEqual(
216-
'1.234 FIL'
216+
'1.23 FIL'
217217
)
218218
expect(new FilecoinNumber('12.3456789', 'fil').formatBalance()).toEqual(
219-
'12.345 FIL'
219+
'12.34 FIL'
220220
)
221221
expect(new FilecoinNumber('123.456789', 'fil').formatBalance()).toEqual(
222-
'123.456 FIL'
222+
'123.45 FIL'
223223
)
224224

225225
// Thousands
@@ -298,13 +298,13 @@ describe('FilecoinNumber', () => {
298298

299299
// Less then 1000
300300
expect(new FilecoinNumber('-1.23456789', 'fil').formatBalance()).toEqual(
301-
'-1.234 FIL'
301+
'-1.23 FIL'
302302
)
303303
expect(new FilecoinNumber('-12.3456789', 'fil').formatBalance()).toEqual(
304-
'-12.345 FIL'
304+
'-12.34 FIL'
305305
)
306306
expect(new FilecoinNumber('-123.456789', 'fil').formatBalance()).toEqual(
307-
'-123.456 FIL'
307+
'-123.45 FIL'
308308
)
309309

310310
// Thousands

0 commit comments

Comments
 (0)
Please sign in to comment.