score:9

Accepted answer

have you tried using d3.locale? it would work great for your use case because it builds the formatting functions based on your own localization rules.

what you can do is create custom localization rules that give your format:

var mylocale = {
  "thousands": ".",  // specify that thousands are separated with a dot
  // .. other rules
}

and then use those rules to initialize your custom formatter:

var localeformatter = d3.locale(mylocale);
// ',.2r' means grouped thousands with two significant digits. 
// by default ',' means 'thousands' but we switched that into a '.' in our custom localization
var myformatter = localeformatter.numberformat(',.2r'); 
var value = myformatter(1000000); // "1.000.000"

here's a running example:

// custom localization options
var mylocale = {
  "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": ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"],
  "shortmonths": ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"]

}

// create custom locale formatter from the given locale options
var localeformatter = d3.locale(mylocale);

// create a formatter for the number (grouped thousands with two significant digits). by default ',' means 'thousands' but we switched that into a '.' in our custom localization
var numberformat = localeformatter.numberformat(",.2r");

// test
console.log(numberformat(1000000)); // "1.000.000"
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

score:0

check out the source code for d3plus, in particular the number format library: https://github.com/alexandersimoes/d3plus/blob/master/src/number/format.coffee

there is an example of what you are trying to achieve here: http://d3plus.org/examples/advanced/7760febcda3375b39e1f/

in particular, look at the format() method (lines 29 - 39).

score:0

i think you are looking for d3.numberformat(",d")

score:2

instead of the complexity of a custom number formatter, d3plus has localizations built in, including one for spanish. not only does this take care of number formatting, but it also translates any interface messages into that language. all you'd have to add to your visualization is this:

.format("es_es")

here's an example on the d3plus website: http://d3plus.org/examples/advanced/10bfe1908a200c201145/

and here's a list of the supported localizations: https://github.com/alexandersimoes/d3plus/wiki/localization


Related Query

More Query from same tag