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 option to disable commifying in dn.format function #4

Closed
DannyDelott opened this issue Sep 24, 2023 · 4 comments
Closed

Add option to disable commifying in dn.format function #4

DannyDelott opened this issue Sep 24, 2023 · 4 comments

Comments

@DannyDelott
Copy link

Thedn.format function currently always commifies numbers by default. It would be beneficial to have an option to turn this behavior off. This would provide greater flexibility in formatting number strings and improve the interoperability between dn.from and dn.format.

import * as dn from "dnum";

let b = dn.from("870983127.93887"); // dn.from() can parse strings, numbers, bigint and more

console.log(
  dn.format(b, 2), // Current behavior: "870,983,127.94"
  dn.format(b, { digits: 2, commify: false }), // Desired behavior: "870983127.94"
);
@bpierre
Copy link
Owner

bpierre commented Sep 24, 2023

Good point, I think it would be nice to have this. I am wondering how to do it, as the name commify implies the presence of commas which is only true with some locales. Such an option would also override others (compact and locale), which is something I try to avoid if possible.

From what I understand, your case is that you need to store the numbers somewhere as a string, but in a way that is a bit more flexible than toJSON() (which requires to use dnum again to display the number).

Since we already have toNumber(), maybe we could add toString()? It would work in a very similar way:

let value = [123456789000000000000000n, 18];

toString(value); // "123456.789"
toString(value, 1); // "123456.8"

What do you think?

@bpierre
Copy link
Owner

bpierre commented Sep 24, 2023

I made a PR with the change: #5

@bpierre bpierre mentioned this issue Sep 24, 2023
@DannyDelott
Copy link
Author

Good point, I think it would be nice to have this. I am wondering how to do it, as the name commify implies the presence of commas which is only true with some locales. Such an option would also override others (compact and locale), which is something I try to avoid if possible.

From what I understand, your case is that you need to store the numbers somewhere as a string, but in a way that is a bit more flexible than toJSON() (which requires to use dnum again to display the number).

Since we already have toNumber(), maybe we could add toString()? It would work in a very similar way:

let value = [123456789000000000000000n, 18];

toString(value); // "123456.789"
toString(value, 1); // "123456.8"

What do you think?

This is perfect, thanks! We're moving away from formatUnits and commify from ethers to a web3-agnostic formatter and this .toString() would give us exactly what we need for a drop-in replacement. 🤘

@bpierre
Copy link
Owner

bpierre commented Sep 25, 2023

Awesome! It’s published: https://github.com/bpierre/dnum/releases/tag/v2.9.0

@bpierre bpierre closed this as completed Sep 25, 2023
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

No branches or pull requests

2 participants