Master Advanced PDF CRE Templates: Formatting a Number with Proper Comma and Decimal Placement

NetSuite Reporting

Tags: , , , ,

This article is relevant if you are working with NetSuite Advanced PDF templates and you are using the Content Renderer Engine  (CRE) to supercharge your template production.

Background

While working on a CRE template, I found myself with a field reference that needed to be rendered as a currency with proper comma and decimal placement. In most cases, my currency number values were already properly formatted before passing through the CRE to the template. But in this case, my field value needed proper comma and decimal placement as well as a currency symbol.

A Custom Number Formatting Function

.numberWithCommas()

There are a few algorithms that one could find to handle the comma and decimal task. Here is a function that is rather effective. This function does not return the ‘$’ currency symbol; however, this can be handled easily in the HTML code.

Number.prototype.numberWithCommas = function(){
  return this.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
};

Now you might ask, “How do I implement this little function, and how do I call it from within my template?” Well, there’s a feature in the CRE that allows linking to an external JavaScript file. This feature is called JavaScript Override.

Using The JavaScript Override in The CRE to Add and Call A Custom Number Format Function

To access the JavaScript Override section, open up a selected CRE Profile, find the JAVASCRIPT OVERRIDE section as shown in the screen capture, and click the EDIT link. An editor window for the linked JavaScript file will be displayed. This file is the script file that is linked to the CRE template. Inside are functions that can be called from within the CRE template.

Calling The Custom Number Function

To call my custom number formatting function from within my CRE template, I added code similar to the following snip:

<th colspan="6">Projected Total</th>
<td colspan="6" align="left">
  {if record.projectedtotal}
    $${record.projectedtotal.numberWithCommas()}
  {/if}
</td>

If you noticed, this function does not return the currency symbol. The proper currency symbol, ‘$’ in this case’, is handled outside of the function with an extra ‘$’ before the variable name. You could modify this function to return the currency symbol. And if needed, you could even modify this function to handle foreign currencies. For this project, the USD ‘$’ was all I needed.

Accelerate your NetSuite PDF and HTML Development

If you are frustrated by NetSuite’s implementation of the Advanced PDF tool, we offer the Content Renderer Engine to help you accelerate your development giving you control, flexibility and more rapid development.  Contact us to discuss your application.

Be Sociable, Share!

Marty Zigman

Holding all three official certifications, Marty is regarded as the top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. He is a former Deloitte & Touche CPA and has held CTO roles. For over 30 years, Marty has produced leadership in ERP, CRM and eCommerce business systems. Contact Marty to set up a conversation.

More Posts - Website - Twitter - Facebook - LinkedIn - YouTube

About Marty Zigman

Holding all three official certifications, Marty is regarded as the top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. He is a former Deloitte & Touche CPA and has held CTO roles. For over 30 years, Marty has produced leadership in ERP, CRM and eCommerce business systems. Contact Marty to set up a conversation.

Biography • Website • X (Twitter) • Facebook • LinkedIn • YouTube

2 thoughts on “Master Advanced PDF CRE Templates: Formatting a Number with Proper Comma and Decimal Placement

  1. Alien says:

    You should be able to do that without writing a function, this work on Advanced PDF Template.

    $${record.projectedtotal?string(“,##0.000000”)}

    Kind Regards
    Alien

Leave a Reply

Your email address will not be published. Required fields are marked *