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 any string suffix to number. #121

Open
inesmoskal opened this issue Aug 5, 2021 · 1 comment
Open

Add any string suffix to number. #121

inesmoskal opened this issue Aug 5, 2021 · 1 comment

Comments

@inesmoskal
Copy link

I know there is the possibility to add the currency suffix, but it would be useful to be able to add a more flexible suffix. For example "°C" for temperature values or "kg".

@mbostock
Copy link
Member

mbostock commented Aug 5, 2021

For a fixed suffix, the recommended way is to wrap the format function. For example:

const formatNumber = d3.format("d"); 
const formatCelsius = d => `${formatNumber(d)}°C`;

You can also combine the two like so:

const formatCelsius = (f => d => `${f(d)}°C`)(d3.format("d"));

We could perhaps formalize this pattern as:

const formatCelsius = d3.format("d", (f, d) => `${f(d)}°C`);

Or maybe make the arguments order-dependent to allow pre- (e.g., multiplying by 100) and post-formatting:

const formatCelsius = d3.format("d", s => `${s}°C`);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants