Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.JS: Format as currency euro

Tags:

d3.js

The D3 Wiki describes formatting, and how it's possible to format your numbers to dollar values using d3.format("$.,2f");. However, using d3.format("€,.2f"); to format numbers to euro values does not work. What is the appropriate way to do this?

like image 550
TheLeonKing Avatar asked Jan 07 '23 20:01

TheLeonKing


1 Answers

2016-07-06 edit: The answer below was valid for d3 v3. As of d3 v4, d3.formatLocale looks to be the equivalent to what d3.locale was.


The dollar sign should be kept, as it means "currency".

What you need to do is define the locale (cf. documentation) using d3.locale() function (d3 defaults to en-US).

Here is an example where en-US parameters are kept for all properties, except for the currency, where the $ prefix got replaced by the suffix ( done with: "currency": ["", "€"]):

var NL = d3.locale ({
  "decimal": ".",
  "thousands": ",",
  "grouping": [3],
  "currency": ["", "€"],
  "dateTime": "%a %b %e %X %Y",
  "date": "%m/%d/%Y",
  "time": "%H:%M:%S",
  "periods": ["AM", "PM"],
  "days": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  "shortDays": ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  "months": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  "shortMonths": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
})

Then you can use NL.numberFormat("$,.2f") to format numbers as Euro.

Working demo.

More information about d3.locale usage in this question.

like image 64
Mehdi Avatar answered Feb 04 '23 16:02

Mehdi