Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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.


2. Create global variable for the desired 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.

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


Thanks for reading!

2 Comments
Labels in this area