Liquid UI - Documentation - 21.01 Convert user entered value to decimal format

21.01 Convert user entered value to decimal format


Prerequisites


Purpose

This article explains how to validate and convert user-entered decimal formats in the SAP GUI by using a pushbutton. Depending on the value entered in the input field, you can convert it to SAP decimal or normal decimal format. To illustrate the conversion of a user-entered value to decimal format, we have taken the SAP Easy Access screen as an example and will walk you through the following steps:

  1. Delete the Image container on the SAP Easy Access screen
  2. Add input fields to enter the values
  3. Add a pushbutton that will run a function that changes the input decimal value entered into the user format when clicked
  4. Add a function that converts user-entered decimal numbers to user format

User Interface

//Create this file inside your script folder for customizing the SAP Easy Access screen: SAPLSMTR_NAVIGATION.E0100.sjs
//Now, let's start adding the Liquid UI script to the above file and save it.

Customization

  1. Delete the image container on the SAP screen using the del command.
     
    //Deletes the Image container on the screen
    del("X[IMAGE_CONTAINER]");
    
     
     
  2. Create two input fields and label them with Enter Decimal Value and User Format which allows users to enter the values.
     
    //Creates input fields with labels such as Enter Decimal Value and User Format which allows the user to enter the values.
    inputfield([1,1], "Enter Decimal Value", [1,22], {"name":"z_val", "size":15}); 
    inputfield([2,1], "User Format", [2,22], {"name":"z_val_userformat", "size":15, "readonly":true});
     
     
  3. Add a pushbutton labeled User Decimal Format which will run a function that changes the input decimal value entered to the user format when clicked.
     
    //Creates a pushbutton that runs a function that changes the user-entered decimal format on click
    pushbutton([1,40], "@01@User Decimal Format", {"process":changeUserFormat});
     
     
  4. Add the below script which checks the string value, converts user-entered decimal numbers to user format, and vice-versa.
     
    // Function converts the general decimal format to user decimal format in SAP
    function userSAPDecimalFormat(nStr,nSeparator){
        var str = nStr.split('.');
        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(nSeparator == 'Y' || nSeparator == ' ') {
            return str.join(',');
        } else {
            return str.join('.');
        }        
    }    
    
    // Function to check and convert user entry for decimal format
    function changeUserFormat(){
        if(isBlank(z_val)){
            return('E: Please enter value');
        }
        z_val = decimalNotationFormat(USERDECIMALFORMAT,z_val.toString(),3);
        z_tmp = userSAPDecimalFormat(z_val,USERDECIMALFORMAT);
        set('V[z_val_userformat]','&V[z_tmp]');
    }
    // Remove blank spaces
    String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,'');}
    
    // Function to check if the string value is blank
    function isBlank(jvar){
    	
       if(typeof jvar == 'string') {
          jvar = jvar.trim();
       }
       if(typeof jvar == 'undefined') {
          jvar = '';
    	   
       }
      return(jvar == 'undefined' || jvar == undefined || jvar == null || jvar == "" || jvar == void 0);
    
    }
    // Function converts the user decimal format in SAP to a general decimal format (xxxxxx.xx) 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; } return parseFloat(str.trim()).toFixed(3); } // Read User Decimal Format Defaults if(!READDEFAULTS){ call('BAPI_USER_GET_DETAIL',{'IN.USERNAME':'&V[_user]','OUT.DEFAULTS':'userDefaults'}); USERDECIMALFORMAT = userDefaults.substring(28,29); //set("V[USERDECIMALFORMAT]", userDefaults.substring(28,29)); READDEFAULTS = true; }

SAP Process

  1. To convert a decimal number to SAP decimal format, enter the number in the input field and click the User Decimal Format pushbutton. The number will be converted and displayed in the User Format field.
     


Next Steps

Padding a String
Learn how to add values before the value entered in the inputfield.

10 min.

This article is part of the Take a deep dive into the input field and pushbutton tutorial.


Can't find the answers you're looking for?