1

I am trying to use toLocaleString() in a Pug view. It works when no arguments are provided. It seems to default to 'en-US', irrespective of the browser (language) used. I can live with that, but I would like to use the options, such that 2 decimals are displayed, i.e., as follows:

toLocaleString('en-US', { minimumIntegerDigits: 2 })

In the JavaScript section of the Pug view that works fine. I have also tried using toFixed(2).toLocaleString(), but then it seems like toLocaleString() is ignored.

FYI I am trying to do this in a table, full line of code:

td(align="right")= record.cy.toLocaleString('en-US', { minimumIntegerDigits: 2 })

where record.cy is a Number.

dkreeft
  • 642
  • 5
  • 17

1 Answers1

2

This is due to the fact that pug is run on the server and not in the browser. Your server's locale will remain constant.

Take a look at this question's top answer for more details on how to process this in node.js.

Graham
  • 7,431
  • 18
  • 59
  • 84
  • Thank you, that explains a lot. What I do not get, however, is that it partly works (separators are introduced as per the locale of the server), but the options do not. If Pug converts the file to an HTML file on the server side, why does it not also respect the options set by `toLocaleString()`? – dkreeft Mar 08 '19 at 09:31
  • 1
    @Victoria The `minimumIntegerDigits` option doesn't control how many decimals are displayed. For that, you want `minimumFractionDigits`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString – Sean Mar 09 '19 at 00:34