question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add number formatting utilities

See original GitHub issue

From an offline discussion:

  • formatNumber(): use Intl.formatNumber()
  • formatTokenAmount(): build on top of formatNumber() and add some options to make it easier to format token amounts (e.g. see this function)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
bpierrecommented, Feb 26, 2020

@ECWireless I think we could adapt them to make it a bit simpler to use:

pow, multiplier and decimals

I think we could replace these three params by a single decimals = 18, since it is both defined by ERC-20 and widely used. Similarly to amount, this should be either a number, or any object with a toNumber() method (i.e. we should call Number() on it).

amount

This should be either a string, a BigInt instance, or any object with a .toString() method (i.e. we should call String() on it).

rounding

I think we could rename this to something like displayDecimals, roundToDecimals, decimalPlaces, or just precision?

isIncoming

I don’t think this is needed, we can use a sign on the BigInt + displaySign (or signDisplay) to determine that.

displaySign

We might want to name it signDisplay to align it with the new Intl.NumberFormat API.


@sohkai @ECWireless what would you guys think about naming the function formatAmount()? It might sound a bit generic, but I think there is enough context for authors to know what it is about. It would also make it clearer that we can use it to format currencies like USD as well.

0reactions
sohkaicommented, Apr 10, 2020

Closed with #728

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a custom number format - Microsoft Support
Create and build a custom numeric format to show your numbers as percentages, currency, dates, and more. ... Add a space to separate...
Read more >
Number formatting utility for Node and Browser - GitHub
Format ; options.round, Number|Falsy, Precision (e.g. 0.001 for 3 decimal places or 1000 for rounding to thousands, use 1 to round to whole...
Read more >
Create a custom cell format in Numbers on Mac - Apple Support
In the Format sidebar, click the Cell tab. Click the Data Format pop-up menu, then choose Create Custom Format. Type a name for...
Read more >
Custom Excel number format - Ablebits
Select a cell for which you want to create custom formatting, and press Ctrl+1 to open the Format Cells dialog. Under Category, select...
Read more >
How To Enforce Form Field Number Formatting - YouTube
How To Enforce Form Field Number Formatting - Insert Help Text ... tool form fields, you can enforce certain number formatting to keep...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found