Convert an integer to a formatted currency string for use in an html page

Convert an integer to a formatted currency string for use in an html page

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,
  1. deluge doesn't have any number conversion tasks built-in so we're stuck with custom functions returning integers or floats
  2. 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
  3. 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.

i will not take credit for #1, instead i'll give a shout out to @cwm2010 for the script he recommended in a response to another community member's question.

here is the makeRange custom function you will need to call in order to create a range in my converter script:
  1. list utilities.makeRange(int start, int end)
  2. {
  3.    if(start > end)
  4.    {
  5.       return {};
  6.    }
  7.    else
  8.    {
  9.       lst = List:Int();
  10.       lst.add(start);
  11.       lst.addAll(thisapp.utilities.makeRange(start + 1,end));
  12.       return lst;
  13.    }
  14. }

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.

  1. string utilities.convertIntegerToCurrency(int number)
  2. {
  3.    separator = ",";   // --- specify your type of separator (i'm using a comma)
  4.    divisor = 3;   // specify how many digits in between each separator
  5.    // --- convert integer to string & remove hyphen if number is negative
  6.    string = number.toString().remove("-");
  7.    // --- get length of string
  8.    stringLen = string.length();
  9.    info "string length = " + stringLen;
  10.    separators = 0;
  11.    // --- calculate how many separators will be needed to construct your currency string
  12.    // ------ 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)
  13.    if(stringLen / divisor = (stringLen / divisor).floor())
  14.    {
  15.       separators = (stringLen / divisor) - 1;
  16.    }
  17.    // ------ 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)
  18.    else
  19.    {
  20.       separators = (stringLen / divisor).floor();
  21.    }
  22.    info "number of commas needed =" + separators;
  23.    // --- build your List needed for 'for each' loop
  24.    // ------ makeRange is a custom function required to build the List
  25.    range = thisapp.utilities.makeRange(1,separators);
  26.    info "range = " = range;
  27.    stringBuild = "";   // --- declare stringBuild variable used to continually construct the sting
  28.    // --- execute 'for each' loop to parse through the number and add separators
  29.    for each  i in range
  30.    {
  31.       // --- append separator to front of substring and add suffix of stringBuild value from previous loop iteration
  32.       stringBuild = separator + string.substring(stringLen - i * divisor,stringLen - (i - 1) * divisor) + stringBuild;
  33.    }
  34.    // --- get the first set of leading digits before the first separator
  35.    stringBuild = string.substring(0,stringLen - separators * divisor) + stringBuild;
  36.    // --- now let's bring it all together
  37.    // ------ 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)
  38.    if(number < 0)
  39.    {
  40.       numConverted = "($" + stringBuild + ")";  // --- parentheses formatting for negative #'s
  41.    }
  42.    else
  43.    {
  44.       numConverted = "$" + stringBuild;
  45.    }
  46.    return numConverted;
  47. }

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

    Access your files securely from anywhere

        Zoho Developer Community




                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now