Commit 986db18d authored by Noah Zinsmeister's avatar Noah Zinsmeister

pad sig figs with trailing 0s

remove minimumDecimalPlaces argument
parent 7309e5dc
......@@ -15,8 +15,7 @@ export function formatSignificant(
denominator: BigintIsh = ONE,
significantDigits: number,
format: object = { groupSeparator: '' },
roundingMode: number = Decimal.ROUND_HALF_UP,
minimumDecimalPlaces: number = 0
roundingMode: number = Decimal.ROUND_HALF_UP
): string {
invariant(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`)
invariant(significantDigits > 0, `${significantDigits} isn't positive.`)
......@@ -28,7 +27,9 @@ export function formatSignificant(
.div(denominatorParsed.toString())
.toSignificantDigits(significantDigits)
return quotient.toFormat(
minimumDecimalPlaces > quotient.decimalPlaces() ? minimumDecimalPlaces : quotient.decimalPlaces(),
quotient.precision(true) >= significantDigits
? quotient.decimalPlaces()
: significantDigits - (quotient.precision(true) - quotient.decimalPlaces()),
format
)
}
......
......@@ -77,12 +77,11 @@ describe('entities', () => {
expect(route.midPrice.formatSignificant(2)).toEqual('1200')
expect(route.midPrice.formatSignificant(3)).toEqual('1230')
expect(route.midPrice.formatSignificant(4)).toEqual('1234')
expect(route.midPrice.formatSignificant(5)).toEqual('1234')
expect(route.midPrice.formatSignificant(4, { groupSeparator: ',' })).toEqual('1,234')
expect(route.midPrice.formatSignificant(4, undefined, undefined, 2)).toEqual('1234.00')
expect(route.midPrice.formatSignificant(5)).toEqual('1234.0')
expect(route.midPrice.formatSignificant(5, { groupSeparator: ',' })).toEqual('1,234.0')
expect(route.midPrice.invert().formatSignificant(1)).toEqual('0.0008')
expect(route.midPrice.invert().formatSignificant(2)).toEqual('0.00081')
expect(route.midPrice.invert().formatSignificant(3)).toEqual('0.00081')
expect(route.midPrice.invert().formatSignificant(3)).toEqual('0.000810')
expect(route.midPrice.invert().formatSignificant(4)).toEqual('0.0008104')
expect(route.midPrice.invert().formatSignificant(4, undefined, 1)).toEqual('0.0008103')
expect(route.midPrice.invert().formatSignificant(5)).toEqual('0.00081037')
......@@ -93,6 +92,8 @@ describe('entities', () => {
expect(route.midPrice.formatFixed(2, { groupSeparator: ',' })).toEqual('1,234.00')
expect(route.midPrice.invert().formatFixed(0)).toEqual('0')
expect(route.midPrice.invert().formatFixed(1)).toEqual('0.0')
expect(route.midPrice.invert().formatFixed(2)).toEqual('0.00')
expect(route.midPrice.invert().formatFixed(3)).toEqual('0.001')
expect(route.midPrice.invert().formatFixed(4)).toEqual('0.0008')
expect(route.midPrice.invert().formatFixed(5)).toEqual('0.00081')
expect(route.midPrice.invert().formatFixed(6)).toEqual('0.000810')
......@@ -123,7 +124,7 @@ describe('entities', () => {
).toEqual(inputAmount.toString())
expect(trade.nextMidPrice.formatSignificant(18)).toEqual('1.38958368072925352')
expect(trade.nextMidPrice.invert().formatSignificant(18)).toEqual('0.71964')
expect(trade.nextMidPrice.invert().formatSignificant(18)).toEqual('0.719640000000000000')
expect(trade.slippage.formatSignificant(18)).toEqual('-16.8751042187760547')
......@@ -152,7 +153,7 @@ describe('entities', () => {
).toEqual(trade.inputAmount.toString())
expect(trade.nextMidPrice.formatSignificant(18)).toEqual('1.38958368072925352')
expect(trade.nextMidPrice.invert().formatSignificant(18)).toEqual('0.71964')
expect(trade.nextMidPrice.invert().formatSignificant(18)).toEqual('0.719640000000000000')
expect(trade.slippage.formatSignificant(18)).toEqual('-16.8751042187760547')
......@@ -175,7 +176,7 @@ describe('entities', () => {
const trade = new Trade(route, '1', TradeType.EXACT_INPUT)
expect(trade.slippage.formatSignificant(18)).toEqual(
tokens[1].decimals === 9 ? '-0.300000099400899902' : '-0.3000000000000001'
tokens[1].decimals === 9 ? '-0.300000099400899902' : '-0.300000000000000100'
)
}
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment