Number Abbreviation & Currency Formatter Function

Number Abbreviation & Currency Formatter Function

Here is a handy script for SAP Design Studio users to abbreviate large numbers with currency.

I have created a Formatter Function on a data-bound text box, but one could easily modify this code to be triggered On Click event (ex. button).

 

Steps:

1. Bind text value to cell in data source.

2. Create global variable for currency.

3. Create a Formatter Function

    and paste the following:

/***************************************
* currFormatter
* written by Pratham Mohapatra
 * >> http://prath.co
***************************************/


// Float as string --> replace value with value in formatting Function
var Float_str = "" + value;


// Float to Int --> used in very large number condition
var newInt = Math.floor(value);
// Int to String --> used in very large number condition
var Int_str = "" + newInt;




// Var for negative compensation
var neg = 1;


// Cache negative condition
if (value < 0) {
  neg = -1;
  value = (neg)*(value);
}



// Dummy Var for decimal array
var Float_arr = [""]; Float_arr.pop();


// Suffix Var
var suffix = "";

// Precision Var
var prec = 1;




/* ************************************
* Range Conditional
**************************************/
// Regular Range
if (value < Math.pow(10, 15)) {
  // Hundreds
  if (value < 1000) {

  // Zero Condition - No Decimal
  if (value == 0) {
  Float_str = "" + 0;
  }

  // Declare Decimal Precision
  prec = 2;
  }

  else {
  // Thousands - K
  if (Math.pow(10, 3) <= value && value < Math.pow(10, 6)) {
  // Divide Float by scaling factor --> string
  Float_str = "" + (value/(Math.pow(10, 3)));

  // Declare Suffix
  suffix = " K";
  }

  // Millions - M
  else if (Math.pow(10, 6) <= value && value < Math.pow(10, 9)) {
  // Divide Float by scaling factor --> string
  Float_str = "" + (value/(Math.pow(10, 6)));

  // Declare Suffix
  suffix = " M";
  }

  // Billions - B
  else if (Math.pow(10, 9) <= value && value < Math.pow(10, 12)) {
  // Divide Float by scaling factor --> string
  Float_str = "" + (value/(Math.pow(10, 9)));

  // Declare Suffix
  suffix = " B";
  }
  // Trillions - T
  else if (Math.pow(10, 12) <= value && value < Math.pow(10, 15)) {
  // Divide Float by scaling factor --> string
  Float_str = "" + (value/(Math.pow(10, 12)));

  // Declare Suffix
  suffix = " T";
  }
  }

  // Decimal Condition
  if (Float_str.indexOf(".") !== -1) {
  Float_arr = Float_str.split(".");
  Float_str = Float_arr[0] + "." + (Float_arr[1]).substring(0,prec);
  }

  // Declare formattedValue
  formattedValue = Float_str + suffix;
}


// Very Large Number --> Scientific Notation: num x 10^
else {
  // Declare formattedValue
  formattedValue = Int_str.substring(0,1) + "." + Int_str.substring(1,2) + " E" + (Int_str.length);
}




// Return Abbreviated Value (Negative compensation)
if (neg == 1) {
  // --> change to simple return with formattedValue
  return curr + " " + formattedValue;
}
else if (neg == -1) {
  // --> change to simple return with formattedValue
  return "- " + curr + " " + formattedValue;
}




/***************************************
* written by Pratham Mohapatra
 * >> http://prath.co for more handy scripts
***************************************/

 

4. Execute the application and preview.

Note: formatted version will not be shown in the Design Studio editing view.

 

I hope you find this helpful. Thanks for reading!