0

I have a requirement to convert a salary value: 100000 into 1,000.00. Salary is in double data type.

I am using Groovy Script in this project but I have zero knowledge on how to use it. I am not sure on how to use it or to declare it. Can you guys help me? Thanks!

Then I tried a snippet from a Java program

    DecimalFormat df = new DecimalFormat("#,###.##");
    String input = "1234567890.123456";
    double d = Double.parseDouble(input);
    System.out.println(df.format(d));       // 1,234,567,890.12
    System.out.println(dfGerman.format(d)); 

Here is the code I tried in Groovy Script in CPI

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.text.NumberFormat;

//Add Comma to Salary

def salaryFormat (double bnkAccount){
    DecimalFormat df = new DecimalFormat("#,###.##");
    double d = Double.parseDouble(input);
    return d;
}

def Message processData(Message message) {
    
    //creating the XML file/message
    String str_body, email, a, d;
    String [] str;
    def body = message.getBody(java.lang.String);    
    def root = new XmlSlurper().parseText(body);
    for(int i = 0; i<root.EmpJob.size(); i++)
    {
     str_body = "<root>"+'\n' + "<record>" +'\n';
     str_body = str_body + "<Salary><![CDATA["+salaryFormat(root.EmpJob[i].User.salary.text())+"]]></Salary>"+'\n';
     str_body = str_body + "</record>"+'\n';
     str_body = str_body + "</root>"
    }

    message.setBody(str_body);
    return message;
}

This is the error I was getting:

javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: script1__Script.groovy: 10: unable to resolve class java.util.text.NumberFormat @ line 10, column 1. import java.util.text.NumberFormat; ^ 1 error , cause: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: script1__Script.groovy: 10: unable to resolve class java.util.text.NumberFormat @ line 10, column 1. import java.util.text.NumberFormat; ^ 1 error

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • the error message is self explainable: `unable to resolve class java.util.text.NumberFormat` - there is no such class in java/groovy. probably you want `java.text.NumberFormat` ? – daggett Nov 05 '22 at 16:56
  • btw. it's not clear why you importing it - there is not usage of NumberFormat class in your code... – daggett Nov 05 '22 at 16:57
  • i removed the java.text.NumberFormat then it didnt have any error. i am asking is if my groovy script here is correct because i just copied the exact java code, or is there a specific way to code this using groovy? – Lloyd Albert Tongco Nov 06 '22 at 14:33
  • Usually java code is compatible with groovy. – daggett Nov 06 '22 at 14:53

0 Answers0