TL;DR - the deluge custom scripts below can convert a plain integer into a currency-formatted string, i.e. convert -12345678 to ($12,345,678) or 12345678 to $12,345,678
---------------------------------------------------------------------------------------------------
hey there fellow zoho community members!
i have been developing custom html pages for my apps in zoho creator and came across the challenge of formatting integers into nice and clean currency values. i struggled with this for awhile so i felt others may find this useful
scenario: my custom function takes in a html int parameter projectID, looks up the project record using projectID, then loops through children records and sums up currency values. the custom function outputs an integer. i then take that integer and display it on an html page. however, i want that integer to be displayed in USD currency format. for example:
function output
1553847 or -2378495
desired value displayed on page
$1,553,847 or ($2,378,495)
unfortunately,
- deluge doesn't have any number conversion tasks built-in so we're stuck with custom functions returning integers or floats
- we also cannot use javascript in zoho creator html pages for formatting numbers as zoho blocks any third-party scripts from being invoked from external sources
- also, deluge's support for regex seems to be very limited as well and only offers "match" so i had no "replace" regex options to use
with that said, i had to seek other solutions. i ended up building a deluge script to convert the integer into a USD formatted string value. i'm not a developer by trade so my code may not be the most elegant or efficient so i'd love to hear any suggestions from you and the rest of the community on how to improve this function. at the end of the day, it outputs the result i need for displaying formatted currency values in my html pages.
this method requires two functions: 1) a script to create a List of values to use in a for loop, 2) the script for looping through the original number and appending your desired separators.
here is the makeRange custom function you will need to call in order to create a range in my converter script:
- list utilities.makeRange(int start, int end)
- {
- if(start > end)
- {
- return {};
- }
- else
- {
- lst = List:Int();
- lst.add(start);
- lst.addAll(thisapp.utilities.makeRange(start + 1,end));
- return lst;
- }
- }
the makeRange script will output a List with a range that you will use (line 25 below) for looping through your integer. now here is the script for converting an integer into a currency-formatted string.
- string utilities.convertIntegerToCurrency(int number)
- {
- separator = ","; // --- specify your type of separator (i'm using a comma)
- divisor = 3; // specify how many digits in between each separator
- // --- convert integer to string & remove hyphen if number is negative
- string = number.toString().remove("-");
- // --- get length of string
- stringLen = string.length();
- info "string length = " + stringLen;
- separators = 0;
- // --- calculate how many separators will be needed to construct your currency string
- // ------ if equally divisible 3, reduce number of separators by one (basically checks to see if number of digits is exactly divisible by 3 which means there will be one less separator)
- if(stringLen / divisor = (stringLen / divisor).floor())
- {
- separators = (stringLen / divisor) - 1;
- }
- // ------ if there is a remainder after dividing by 3, set the # of separators (basically, this means there is 1 or 2 digits at the front of the number, in front of the very first separator)
- else
- {
- separators = (stringLen / divisor).floor();
- }
- info "number of commas needed =" + separators;
- // --- build your List needed for 'for each' loop
- // ------ makeRange is a custom function required to build the List
- range = thisapp.utilities.makeRange(1,separators);
- info "range = " = range;
- stringBuild = ""; // --- declare stringBuild variable used to continually construct the sting
- // --- execute 'for each' loop to parse through the number and add separators
- for each i in range
- {
- // --- append separator to front of substring and add suffix of stringBuild value from previous loop iteration
- stringBuild = separator + string.substring(stringLen - i * divisor,stringLen - (i - 1) * divisor) + stringBuild;
- }
- // --- get the first set of leading digits before the first separator
- stringBuild = string.substring(0,stringLen - separators * divisor) + stringBuild;
- // --- now let's bring it all together
- // ------ check to see if number is positive or negative and format accordingly (you can change how you want to handle negative numbers, I wrap them in parentheses but you can modify this code to add a hyphen as the prefix, with no parentheses)
- if(number < 0)
- {
- numConverted = "($" + stringBuild + ")"; // --- parentheses formatting for negative #'s
- }
- else
- {
- numConverted = "$" + stringBuild;
- }
- return numConverted;
- }
there you go, now you have a working function for converting a integer into a currency-formatted string. i hope this is helpful for you. happy to answer any questions you have about my approach to this script and am open to suggestions on how to improve!
enjoy!
ch