How can I format a double in CPI groovyscript?

196 Views Asked by At

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

0

There are 0 best solutions below