Link Search Menu Expand Document

numberFormat - Javascript-Snippets

PDF from HTML Template sample in Javascript-Snippets demonstrating ‘numberFormat’

numberFormat.desc.txt
<code>numberFormat</code> takes a numerical value as input and formats it as a string with a specified decimal length, thousands separator, and decimal separator.
Inserts numberFormat that you can use like this: <code>${{numberFormat macros}}</code>
numberFormat.txt
Handlebars.registerHelper('numberFormat', numberFormat);

function numberFormat(value) {
    // Helper parameters
    var dl = 2;
    var ts = ',';
    var ds = '.';

    // Parse to float
    var value = parseFloat(value);

    // The regex
    var re = '\\d(?=(\\d{3})+' + (dl > 0 ? '\\D' : '$') + ')';

    // Formats the number with the decimals
    var num = value.toFixed(Math.max(0, ~~dl));

    // Returns the formatted number
    return (ds ? num.replace('.', ds) : num).replace(new RegExp(re, 'g'), '$&' + ts);
}

PDF.co Web API: the Web API with a set of tools for documents manipulation, data conversion, data extraction, splitting and merging of documents. Includes image recognition, built-in OCR, barcode generation and barcode decoders to decode bar codes from scans, pictures and pdf.

Get your PDF.co API key here!

Download Source Code (.zip)

return to the previous page explore PDF from HTML Template endpoint