Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatTokenAmount() and formatNumber() #728

Merged
merged 18 commits into from
Apr 10, 2020
Merged

Add formatTokenAmount() and formatNumber() #728

merged 18 commits into from
Apr 10, 2020

Conversation

ECWireless
Copy link
Contributor

Adding a formatNumber() and formatTokenAmount() utility for issue #719

@welcome
Copy link

welcome bot commented Mar 4, 2020

Thanks for opening this pull request! Someone will review it soon 🔍

@auto-assign auto-assign bot requested a review from bpierre March 4, 2020 20:29
@ECWireless ECWireless changed the title Add number formatting utilities #719 Add number formatting utilities Mar 4, 2020
Copy link
Contributor

@sohkai sohkai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we didn't do it on the format.js file, but it'd be great to add a bit of JSDoc to it :).

@ECWireless ECWireless requested a review from sohkai March 10, 2020 12:37
@ECWireless
Copy link
Contributor Author

Wondering if there are any other changes I need to make?

Copy link
Contributor

@Evalir Evalir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boom! Lookin' fine 🔥. Just some comments to improve this further!

src/utils/format.js Outdated Show resolved Hide resolved
src/utils/format.js Outdated Show resolved Hide resolved
src/utils/format.js Outdated Show resolved Hide resolved
src/utils/math.js Outdated Show resolved Hide resolved
Copy link
Contributor

@bpierre bpierre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @ECWireless!

I left one comment about supporting big integers, and I also wanted to ask if you could add some tests to the PR? You can find an example here: https://github.com/aragon/aragon-ui/blob/master/src/utils/color.test.js

src/utils/format.js Outdated Show resolved Hide resolved
@bpierre bpierre requested review from sohkai and removed request for sohkai March 31, 2020 16:37
Copy link
Contributor

@sohkai sohkai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌🙌🙌I can't wait to use this everywhere!! No more of my BN.js hacks running around!

src/utils/format.js Outdated Show resolved Hide resolved
src/utils/format.js Outdated Show resolved Hide resolved
src/utils/format.js Outdated Show resolved Hide resolved
src/utils/format.js Show resolved Hide resolved
src/utils/format.js Outdated Show resolved Hide resolved
src/utils/format.js Show resolved Hide resolved
src/utils/format.js Outdated Show resolved Hide resolved
src/utils/format.test.js Outdated Show resolved Hide resolved
@aragon aragon deleted a comment from sohkai Apr 9, 2020
@bpierre bpierre merged commit 40f11e1 into aragon:master Apr 10, 2020
@welcome
Copy link

welcome bot commented Apr 10, 2020

Congrats on merging your first pull request! Aragon is proud of you 🦅 Eagle gif

return [...integer].reduceRight(
(result, digit, index, { length }) => {
const position = length - index - 1
if (position > 0 && position % 3 === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah jeez, I thought reduceRight() would change the index so that it started "backwards" from 0. That was one of the main simplifications I had wanted with it :).

What do you think instead of doing this with [...integer].reverse().reduce()? That way the position is the index :).

amount,
decimals = 18,
roundToDecimals = 2,
{ symbol = '', displaySign = false } = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I missed this in the first pass around... but I wonder if it would be worth making decimals and roundToDecimals part of the options object?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in https://github.com/aragon/aragon-ui/pull/760/commits/7ed7f5109073ba0cbca45c83ba279cca569a9325

  • Removed the default 18 from decimals to make it a required parameter.
  • roundToDecimals has moved into options.digits.

})

test('should handle large numbers without decimals', () => {
expect(formatTokenAmount(BigInt('2839000000000000000000'), 18, 8)).toEqual(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worthwhile adding a test like formatTokenAmount(BigInt('2839000000010000000000'), 18, 8) or something, just to see the decimal truncation after the last significant digit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and good find! It made visible an issue with the way leading & trailing zeros were handled: https://github.com/aragon/aragon-ui/pull/760/commits/6435c36c47a6df33b7e6ac1abd0c0a976f00e3a2


const amountConverted = divideRoundBigInt(
amount,
JSBI.exponentiate(_10, JSBI.subtract(decimals, roundToDecimals))
Copy link
Contributor

@sohkai sohkai Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering what happens if this goes negative. Should we guard against this and min it at 0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* @param {BigInt|string|number} divisor Divisor
* @returns {string}
*/
function divideRoundBigInt(dividend, diviser) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, we forgot to update the parameter name here :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ECWireless ECWireless deleted the add-number-formatting-utilities branch April 10, 2020 13:09
@sohkai sohkai mentioned this pull request Apr 12, 2020
@bpierre bpierre changed the title Add number formatting utilities Add formatTokenAmount() and formatNumber() Apr 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants