2

I'm using Pygal (with Python / Flask) relatively successfully in regards to loading data, formatting colors, min/max, etc., but can't figure out how to format a number in Pygal using dollar signs and commas.

I'm getting 265763.557372895 and instead want $265,763.

This goes for both the pop-up boxes when hovering over a data point, as well as the y-axes.

I've looked through pygal.org's documentation to no avail. Does anyone know how to properly format those numbers?

UPDATE:

I'm not quite ready to mark this question "answered" as I still can't get the separating commas. However, I did find the following native formatting option in pygal. This eliminates trailing decimals (without using Python's int()) and adds a dollar sign:

graph.value_formatter = lambda y: "$%.0f" % y

Change the 0f to 2f if you prefer two decimals, etc.

elPastor
  • 8,435
  • 11
  • 53
  • 81

1 Answers1

3

graph.value_formatter = lambda y: "{:,}".format(y) will get you the commas.

graph.value_formatter = lambda y: "${:,}".format(y) will get you the commas and the dollar sign. Note that this formatting seems to be valid for Python 2.7 but would not work on 2.6.

Sam
  • 147
  • 11
  • 1
    And `.value_formatter = lambda y: "${:,.0f}".format(y)` will eliminate the trailing decimals. Thank you Sam! – elPastor Apr 28 '16 at 15:33