Hi Community!

Some time ago, I got one issue in a customer, the situation was following:

The customer needed to send a number + number converted to words from SSFF via CPI to a third-party system.

Architecture Landscape:

Landscape

What’s the problem here? SSFF sends only the number, not the number in words. So only way to do it where in CPI or in the third-party system. but last option was also not possible.

I couldn’t found any function to convert numbers into words in CPI or Groovy (please, feel free to share is there another way/solution). So I write a code in groovy to do it and convert numbers to words (based on differents JAVA codes that I found), and you can also convert it in two differents languages, Spanish and English.

Follow the groovy code (Input number it’s in p_salary that is a propery defined in the iFlow, output will be in the properties p_salary_txt_sp for Spanish and  p_salary_txt_en for English):

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
        int number = 0;
        //Get property from iflow p_salary
	    map = message.getProperties();
        p_salary = map.get("p_salary");
        number = Integer.parseInt(p_salary);
        
        //Set response number in words in new property p_salary_txt_sp for Spanish
        String resp = numberToWord_sp(number);
        message.setProperty("p_salary_txt_sp", resp);

        //Set response number in words in new property p_salary_txt_sp for English
        String resp = numberToWord_en(number);
        message.setProperty("p_salary_txt_en", resp);

        return message;
 }
 
 //Spanish translate
private static String numberToWord_sp(int number) {
    // variable to hold string representation of number 
    String words = "";
    def unitsArray = ["cero", "un", "dos", "tres", "cuatro", "cinco", "seis", 
                    "siete", "ocho", "nueve", "diez", "once", "doce",
                    "trece", "catorce", "quince", "dieciseis", "diecisiete", 
                    "dieciocho", "diecinueve"] as String[];

    def tensArray = ["cero", "diez", "veinti", "treinta", "cuarenta", "cincuenta",
                    "sesenta", "setenta", "ochenta", "noventa" ];
 
	if (number == 0) {
	    return "cero";
	}
	
	// add minus before conversion if the number is less than 0
	if (number < 0) { 
           // convert the number to a string
           String numberStr = "" + number; 
           // remove minus before the number 
           numberStr = numberStr.substring(1); 
           // add minus before the number and convert the rest of number 
           return "menos " + numberToWord(Integer.parseInt(numberStr)); 
        } 
        
        // check if number is divisible by 1 million
        if (number >= 1000000) {
         if (number >= 1000000 && number < 2000000) {
             int numAux = number / 1000000;
	          words += numberToWord(numAux) + " millon ";
	          number %= 1000000;
           }else{
            int numAux = number / 1000000;
	          words += numberToWord(numAux) + " millones ";
	          number %= 1000000;
           }
    	}
	// check if number is divisible by 1 thousand
	if (number >= 1000) {
	  	  if(number == 1000) {
	    words += "mil ";
	    number %= 1000;
	  }else{
	     int numAux = number / 1000;
	     if (numAux == 1){
	       words += "mil ";
	       number %= 1000;
	     }else{
	     words += numberToWord(numAux) + " mil ";
	     number %= 1000;
	     }
	  }
	}
	// check if number is divisible by 1 hundred
	if (number >= 100) {
	  if(number == 100) {
	    words += "cien ";
	    number %= 100;
	  }else{
        // Set words for exceptional spells in spanish like 500
	     int numAux = number / 100;
	     if (numAux == 1){
	       words += "ciento ";
	       number %= 100;
	     }else if (numAux == 5){
	       words += "quinientos ";
	       number %= 100;
	     }else if (numAux == 9){
	       words += "novecientos ";
	       number %= 100;
	     }else{
	     words += numberToWord(numAux) + "cientos ";
	     number %= 100;
	     }
	  }
	}
 
	if (number > 0) {
	     // check if number is within teens
	    if (number < 20) { 
            // fetch the appropriate value from unit array
                    words += unitsArray[number];
             } else if (number == 20){
               words += "veinte";
             } else if (number < 30 && number > 20 ){
              int numAux = number / 10;
                words += tensArray[numAux]; 
                if ((number % 10) > 0) {
		              words += unitsArray[number % 10];
                }  
               
             }else{ 
                // fetch the appropriate value from tens array
                int numAux = number / 10;
                words += tensArray[numAux];
                if ((number % 10) > 0) {
		              words += " y " + unitsArray[number % 10];
                    }  
	    }
    }
	  return words;
}
   
   // English translate
private static String numberToWord_en(int number) {
    // variable to hold string representation of number 
    String words = "";
    def unitsArray = ["zero", "one", "two", "three", "four", "five", "six", 
                    "seven", "eight", "nine", "ten", "eleven", "twelve",
                    "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", 
                    "eighteen", "nineteen"] as String[];

    def tensArray = ["zero", "ten", "twenty", "thirty", "forty", "fifty",
                    "sixty", "seventy", "eighty", "ninety" ];
 
	if (number == 0) {
	    return "zero";
	}
	
	// add minus before conversion if the number is less than 0
	if (number < 0) { 
           // convert the number to a string
           String numberStr = "" + number; 
           // remove minus before the number 
           numberStr = numberStr.substring(1); 
           // add minus before the number and convert the rest of number 
           return "minus " + numberToWord(Integer.parseInt(numberStr)); 
        } 
        
        // check if number is divisible by 1 million
        if (number >= 1000000) {
            int numAux = number / 1000000;
	          words += numberToWord(numAux) + " million ";
	          number %= 1000000;
	}
	// check if number is divisible by 1 thousand
	if (number >= 1000) {
		int numAux = number / 1000;
	    words += numberToWord(numAux) + " thousand ";
	    number %= 1000;
	}
	// check if number is divisible by 1 hundred
	if (number >= 100) {
    	int numAux = number / 100;
	     words += numberToWord(numAux) + " hundred ";
	     number %= 100;
	}
 
	if (number > 0) {
	     // check if number is within teens
	     if (number < 20) { 
                    // fetch the appropriate value from unit array
                    words += unitsArray[number];
             } else { 
                // fetch the appropriate value from tens array
                int numAux = number / 10;
                words += tensArray[numAux]; 
                //System.out.print(number);
                if ((number % 10) > 0) {
		              words += "-" + unitsArray[number % 10];
                }  
	        }
    }
 	  return words;
}

Any doubt or improvement, please feel free to comment.

Also if you are interested in more Cloud Integration topics, follow the following link: Cloud Integration | SAP | SAP Blogs

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x