number format for different culture settings

2,693 views
Skip to first unread message

Xiangdong

unread,
Oct 29, 2012, 11:07:25 AM10/29/12
to d3...@googlegroups.com
In part of Europe, the number uses ',' as thousands separator instead of '.'. Does the current d3 have setting for it?
Thanks

Mike Bostock

unread,
Oct 29, 2012, 1:04:52 PM10/29/12
to d3...@googlegroups.com
> In part of Europe, the number uses ',' as thousands separator instead of
> '.'. Does the current d3 have setting for it?

This is available in the pre-release version of D3 3.0. However,
you'll need to build a locale-specific version of D3 in order to use
this functionality. For example, in your terminal:

$ git checkout 3.0
$ LOCALE=fr_FR make clean all

Then, to try it out in node:

> var d3 = require("./");
> d3.format(",.2f")(1234.56)

Mike

Daniele Pennati

unread,
Jun 29, 2013, 10:45:54 AM6/29/13
to d3...@googlegroups.com
I'd like to set '.' for thousand and ',' for the decimal separator...
but I haven't undestand how to build a locale-specific version of d3
there is a tutorial somewhere? I didn't found any resource about it

thanks
daniele

Mike Bostock

unread,
Jun 29, 2013, 12:05:03 PM6/29/13
to d3...@googlegroups.com
The “tutorial” is the email you just replied to. You need to clone the d3 git repository and then build in your desired locale:

  cd d3
  LOCALE=ru_RU make clean all

The result of building with the ru_RU locale is shown here:


If you prefer, you could edit the format files directly, but I’d be careful of doing this as they are autogenerated:


Mike

Michael Neutze

unread,
Jun 29, 2013, 2:33:47 PM6/29/13
to d3...@googlegroups.com, mi...@ocks.org
Mike,

I know you're busy and cannot attend all the newbies, so if someone else could jump in for a little more info it would be greatly appreciated as I am struggling at those steps also.
Is there any tutorial that explains how one has to setup a machine (preferrably OSX) to make these things happen.
So far I have installed XCode, node.js and such but still get errors 

Is there some project that allows for web-based custom builts of D3 like http://projects.jga.me/jquery-builder/ ?
What would it take to sponsor someone to build that, e.g. via Kickstarter?
Is someone willing to do a (paid) workshop at graphicalweb.org / html5devconf.com conference this Octobre covering these techniques?

Thanks,
Michael

Mike Bostock

unread,
Jun 29, 2013, 2:44:55 PM6/29/13
to Michael Neutze, d3...@googlegroups.com
So far I have installed XCode, node.js and such but still get errors 

I’m not able to reproduce these errors. I copy and pasted the npm install command followed by the node_modules/.bin/smash command in the email you linked, and it worked fine. You don’t need to make the Makefile executable; it’s read by make. And you don’t strictly need a Makefile at all, but it’s often the most convenient way to run a command multiple times.

In addition to XCode installed, you probably want to install Homebrew:


You can use Homebrew to install node (brew install node) and keep things up-to-date. You will need to edit your /etc/paths file, however, so that whatever binaries npm installs globally are on your $PATH. The contents of /etc/paths should look like this:

/usr/local/share/npm/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

And you can edit /etc/paths in a text editor (but because it’s an administrative file, you may need to enter your password to save your changes). You’ll also need to close your terminal and create a new one after editing /etc/paths so that your changes are reflected in the $PATH environment variable.

Mike

Michael Neutze

unread,
Jun 30, 2013, 4:35:20 AM6/30/13
to d3...@googlegroups.com, Michael Neutze, mi...@ocks.org
Unfortunately, this does NOT work at all.

I am on a fresh installed MacBook Air running OSX 10.8.4
As per the Homebrew instructions I installed the XCode command line tools only (100 MB instead of 1.6 GB)
Homebrew was installed on an admin account.

$ brew doctor

says I am ready to brew

$ brew install node

results in 

"node: Beginning with 0.8.0, this recipe now comes with npm.
It appears you already have npm installed at /usr/local/lib/node_modules/npm.
To use the npm that comes with this recipe, first uninstall npm with
`npm uninstall npm -g`, then run this command again."

so node is already there I assume

I am adding /usr/local/lib/node_modules/npm on top of the /etc/paths entries you stated
works as expected

$ LOCALE=de_DE make clean all

gives me several errors that tell me to install smash and uglify-js, so I do

$ npm install smash
$ npm install uglify-js

both installs ok

then I run again

$ LOCALE=de_DE make clean all

but only get this output

/Users/d3user/d3/node_modules/smash/smash:46
  if (error) throw error;
                   ^
Error: ENOENT, open 'src/format/format-localized.js'
rm -f -- d3.js d3.min.js src/format/format-localized.js src/time/format-localized.js bower.json component.json
node_modules/.bin/smash src/d3.js | node_modules/.bin/uglifyjs - -b indent-level=2 -o d3.js

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: ENOENT, open 'src/format/format-localized.js'

/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:204
    throw new JS_Parse_Error(message, line, col, pos);
          ^
Error
    at new JS_Parse_Error (/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:196:18)
    at js_error (/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:204:11)
    at croak (/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:636:9)
    at token_error (/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:644:9)
    at unexpected (/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:650:9)
    at block_ (/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:943:28)
    at ctor.body (/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:917:25)
    at function_ (/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:922:15)
    at expr_atom (/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:1123:24)
    at maybe_unary (/Users/d3user/d3/node_modules/uglify-js/lib/parse.js:1287:19)
make: *** [d3.js] Error 8


I have closed terminal in between, even restarted the computer, logged out, changed the Computer's name to not contain blanks and set the OS to English, everything to no avail.

Is there anything, Mike, that you can think of, that is different with your computer and that I may have forgotten to add?

Thanks to any hints

Michael

Michael Neutze

unread,
Jul 1, 2013, 3:03:01 PM7/1/13
to d3...@googlegroups.com, Michael Neutze, mi...@ocks.org
Ok, there's a reason they call it RTFM, sorry I lost my temper on this.
Thanks to Mike's endless patience I have now a working setup for custom built D3.
You really have to follow all the instructions painstakingly.

However the number format doesn't work the way it's supposed to, which was the original topic of this thread.

Example: One thousand two hundred dollars and fifty cents

US: 1,200.50
DE: 1.200,50

given

     var money = 1200.5;

it should be formated with D3

   d3.format("n");

and result in the above formating _according_ to the LOCALE the d3.js was created with
however 

  $ LOCALE=de_DE make clean all

now gives me a working d3.js but there's is no thousand separator at all happening with  d3.format("n") where before (default D3.js) there was. The numbers just appear as JavaScript handles them.

Is there some documentation of the different locale settings or am I missing something else here?

Thanks again
Michael

Michael Neutze

unread,
Jul 6, 2013, 4:59:57 PM7/6/13
to d3...@googlegroups.com, Michael Neutze, mi...@ocks.org
I filed a bug report for this
"localized decimalPoint never gets converted when a comma"

CARLOS ANDRES ACOSTA

unread,
May 5, 2015, 4:23:08 PM5/5/15
to d3...@googlegroups.com
Can it is posible chage "," for "." in separate thousand?

Michael Neutze

unread,
May 6, 2015, 1:59:46 PM5/6/15
to d3...@googlegroups.com, carlo...@gmail.com
yes you can, since version 3.4 D3 supports localized number formats,
see here for options
https://github.com/mbostock/d3/wiki/Localization

e.g. for spanish

var ES = d3.locale({
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["", " €"],
dateTime: "%A, %e de %B de %Y, %X",
date: "%d/%m/%Y",
time: "%H:%M:%S",
periods: ["AM", "PM"],
days: ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes",
"sábado"],
shortDays: ["dom", "lun", "mar", "mié", "jue", "vie", "sáb"],
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"]
});

var thsd = ES.numberFormat("n");

thsd(10000); // 10.000

see it in action here
https://www.destatis.de/bevoelkerungspyramide/
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "d3-js" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/d3-js/NH90E7J7IUo/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> d3-js+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages