Hello SCN!


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.

/wp-content/uploads/2016/09/bound_text_1039815.jpg


2. Create global variable for the desired currency.

/wp-content/uploads/2016/09/glo_var_1039816.jpg


3. Create a formatter function…

/wp-content/uploads/2016/09/formatfxn_1039862.jpg


…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.

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

/wp-content/uploads/2016/09/preview_1039863.jpg


Thanks for reading!

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Stein Moshuus

    Thx Pratham! Very usefull!
    I’m struggling with the number format itself. Do you know if one can use the formatter function to alter the number of date formatĀ – i.e. similar to excel cell style # ##0 etc.

    (0) 

Leave a Reply