Author Topic: Mathematical calculation for different decimal notation  (Read 5492 times)

Leo Chu

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 16
    • View Profile
Mathematical calculation for different decimal notation
« on: July 17, 2018, 10:39:15 AM »
Mathematical calculation for different decimal notation

This topic instructs how to perform correct mathematical calculation for different decimal notation.
We'll also cover scenarios when Windows OS uses different decimal notation other than "1,234,567.89".


SAP Default Decimal Notation
SAP provides below decimal notation options for user profile default
   "X":   1,234,567.89
   "Y":   1 234 567,89
   " ":   1.234.567,89

In order for JavaScript to perform calculation, we need to first convert the format to raw format then calculate.
Once we have the result, we need to convert the format back to what SAP requests then assign the value.

Step 1: Create the function to convert specific format value to raw format
// Example: z_val = decimalNotationFormat("Y",Gweightitem.toString(),3);
function decimalNotationFormat(numberFormat,number,nDec){
   var str = "";

   if(nDec == void 0)   // Default for number of decimal places
      nDec = 2;
     
   switch(numberFormat) {
      case 'X':
         str = number.replace(/\,/g, '');      // Replace , with nothing
         break;
         
      case 'Y':
         str = number.replace(/\s+/g,'');      // Remove Blank Spaces
         str = str.replace(/\,/g, '.');         // Replace , with .
         break;
         
      default:
         str = number.replace(/\./g, '');      // Replace . with nothing
         str = str.replace(/\,/g, '.');         // Replace , with .
         break;
   }
   
   if(str.indexOf(".")>-1){
      if((str.length - (str.indexOf(".")+1)) > nDec)
         str = str.substring(0,(str.indexOf(".")+nDec+1));
   }

   return str;
}


Step 2: Create the function to convert raw format value to specific decimal notation format
// Example:  z_value = userSAPDecimalFormat(z_val,"Y");
function userSAPDecimalFormat(nStr,nSeparator){
   var fStr = nStr.toString().replace(/\,/g, '');

   var str = fStr.split('.');
   
   if(str[0].substring(0,1) == "-"){
      var negative_val_flag = true;
      str[0] = str[0].substring(1,str[0].length);
   }
   else{
      var negative_val_flag = false;
   }
   
   var offset = str[0].length % 3;

   if(nSeparator == ' ')
      str[0] = str[0].substring(0, offset) + str[0].substring(offset).replace(/([0-9]{3})/g, ".$1");
   if(nSeparator == 'X')
      str[0] = str[0].substring(0, offset) + str[0].substring(offset).replace(/([0-9]{3})/g, ",$1");
   if(nSeparator == 'Y')
      str[0] = str[0].substring(0, offset) + str[0].substring(offset).replace(/([0-9]{3})/g, " $1");
   
   if(offset == 0)
      str[0] = str[0].substring(1,str[0].length);

   if(negative_val_flag)
      str[0] = "-" + str[0];
   
   if(nSeparator == 'Y' || nSeparator == ' ') {
      return str.join(',');
   } else {
      return str.join('.');
   }     
}


Step 3: Use above functions to build the calculation logic
If Windows OS uses decimal notation setting as "1,234,567.89" but SAP requests"1 234 567,89":
   mm01_mat_weight = decimalNotationFormat("Y", mm01_mat_weight.toString(), 3);      //Convert material weight to raw format
   mm01_mat_qty = decimalNotationFormat("Y", mm01_mat_qty .toString(), 3);         //Convert material quantity to raw format
   mm01_total_weight = parseFloat(mm01_mat_weight) * parseFloat(mm01_mat_qty);      //Calculate material total quantity
   mm01_total_weight = userSAPDecimalFormat(mm01_total_weight, "Y");            //Convert material total quantity to SAP format "Y"


Step 4: If Windows OS doesn't use decimal notation as "1,234,567.89"
If Windows OS uses decimal notation setting as "1 234 567,89"
   mm01_total_weight = parseFloat(mm01_mat_weight.replace(/\s+/g,'')) * parseFloat(mm01_mat_qty.replace(/\s+/g,''));      //Calculate material total quantity
   mm01_total_weight = userSAPDecimalFormat(mm01_total_weight, "Y");            //Convert material total quantity to SAP format "Y"

If Windows OS uses decimal notation setting as "1.234.567,89"
   mm01_total_weight = parseFloat(mm01_mat_weight.replace(/\./g,'')) * parseFloat(mm01_mat_qty.replace(/\./g,''));      //Calculate material total quantity
   mm01_total_weight = userSAPDecimalFormat(mm01_total_weight, " ");            //Convert material total quantity to SAP format " "

Note: parseFloat() takes OS decimal notation format in order to convert, but it becomes raw format "1234567.89" after conversion