KB Article 16004
2019/02/08

Close
X
Contact Tech Support

 Email Tech Support
 (250) 655-1766
 [7:30 - 5:00 PST]
Your Meeting ID will be Provided by a Rep.
Tech Support
Tech Support Home
Software Updates
Knowledge Base Search
Recent KB Articles
Product FAQs
Code Samples
Documentation
Tech Blog
System Requirements
Installation Instructions
Support Testimonials
Contact us to...
Create a Ticket
Request a Free Demo
Suggest a New Feature
Submit Feedback
Upload a Large File
Other Services
Software Training
Professional Services

How to automatically format currency values in Presto

Product: Presto Type: How to

Sometimes we see programs that output a currency value as an unformatted number and need an easy way to format this number to be more readable. In this example we show you how to set up Presto so that you can have currency values automatically formatted on any page by adding a CSS class to the element.

  1. Go to Tools -> Resources -> custom.js and add the following code:
    function formatCurrency()
    {
    	jQuery('.currency').each(function() {
    		var currentElement = jQuery(this);
    		var value = currentElement.text();
    		value = formatAsCurrency(value);
    		currentElement.text(value);
    	});
    }
    
    function formatAsCurrency(value)
    {
    	// config
    	var currencySymbol = '$'; // the currency symbol to use (e.g. $, £, €, ¥)
    	var currencySymbolOnLeft = true; // whether to show the currency symbol on the left or right of the number, true for left, false for right
    	var numDecimalPoints = 2; // how many decimal points to show in the number, will round as needed
    	var numbersBetweenCommas = 3; // how many numbers to have between commas, use 0 or less to skip adding commas
    	var decimalCharacter = '.'; // the character to use for the decimal point
    	var preserveBlanks = true; // whether to leave blank values blank or replace blanks with 0
    	// end config
    	
    	// check for values passed to override defaults
    	// breaks in the case statement were intentionally left out
    	switch (Math.min(arguments.length, 7))
    	{
    		case 7:
    			if (arguments[6] !== null)
    			{
    				preserveBlanks = arguments[6];
    			}
    		case 6:
    			if (arguments[5] !== null)
    			{
    				decimalCharacter = arguments[5];
    			}
    		case 5:
    			if (arguments[4] !== null)
    			{
    				numbersBetweenCommas = arguments[4];
    			}
    		case 4:
    			if (arguments[3] !== null)
    			{
    				numDecimalPoints = arguments[3];
    			}
    		case 3:
    			if (arguments[2] !== null)
    			{
    				currencySymbolOnLeft = arguments[2];
    			}
    		case 2:
    			if (arguments[1] !== null)
    			{
    				currencySymbol = arguments[1];
    			}
    	}
    
    	// strip out non-numeric characters
    	value = value.replace(/[^0-9\.]/g, '');
    	
    	// deal with blank values
    	if (value == '')
    	{
    		if (preserveBlanks)
    		{
    			jQuery(this).text(value);
    			return;
    		}
    		else
    		{
    			value = '0';
    		}
    	}
    
    	// set number of decimal points
    	value = parseFloat(value).toFixed(numDecimalPoints);
    	
    	// trim leading zeroes
    	value = value.replace(/^0*([0-9])/, '$1');
    	
    	if (value == '' || value.substr(0, 1) == '.')
    	{
    		value = '0' + value;
    	}
    	
    	// add commas to number
    	if (numDecimalPoints > 0)
    	{
    		var pattern = new RegExp('\\B(?=(\\d{' + numbersBetweenCommas + '})+(?!\\d))', 'g');
    		var decimalPos = value.indexOf('.');
    		
    		if (decimalPos >= 0)
    		{
    			value = value.substr(0, decimalPos).replace(pattern, ",") + decimalCharacter + value.substr(decimalPos + 1)
    		}
    		else
    		{
    			value = value.replace(pattern, ",");
    		}			
    	}
    	
    	// add currency symbol
    	if (currencySymbolOnLeft)
    	{
    		value = currencySymbol + value;
    	}
    	else
    	{
    		value += currencySymbol;
    	}
    	
    	// replace value of element with formatted version
    	return value;
    }
    
    jQuery(document).on('PageComplete', formatCurrency);
  2. If you want all the currency values to be right aligned, go to Tools -> Modify Skin -> stylesheet.css and add the following code:
    .currency
    {
    	text-align: right;
    	min-width: 100px;
    }
  3. Once all the changes are done, click the Refresh Panel button
  4. In the Visual Editor, add the class "currency" to any fields you want to be automatically formatted.

Rate This Article

Did this example help you to achieve your goal?
 Yes  No  Don't Know

Enter additional comments below.   If you want to hear back from us, include your contact information.

Email Address:
Comments:
Please enable JavaScript in order to rate this page.