Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jade printf-like number formatting

Tags:

pug

How can I control the formatting of numbers in Jade template?

Sample code:

  for item in issue.item
   tr
    td.utilityValue #{item.$.value}
    td.utilityUtil #{item.$.evaluation * utilitySpace.weightmultiplyer}

I want the numbers to print with 2 significant digits.

like image 700
Erel Segal-Halevi Avatar asked Apr 19 '26 22:04

Erel Segal-Halevi


1 Answers

Here is the solution I found; it works for Express 3:

  1. Install sprintf by issuing:

    npm install sprintf
    
  2. In app.js:

    app.locals.sprintf = require('sprintf').sprintf;
    
    app.locals.format = "%+1.0f";
    
  3. In template.jade:

    ...... td.utility #{sprintf(format, value)} ......

like image 138
Erel Segal-Halevi Avatar answered Apr 21 '26 14:04

Erel Segal-Halevi