cancel
Showing results for 
Search instead for 
Did you mean: 

string to decimal

former_member237514
Participant
0 Kudos

Hi All,

I have small requirement like i need to convert string to decimal

I am getting quantity filed as string but target excepting decimal ,how to do this

I tried in google but i didn't get .Plz help me this one

Thanks

Kavitha

Accepted Solutions (1)

Accepted Solutions (1)

former_member182412
Active Contributor
0 Kudos

Hi Kavitha,

Can you give us the example?

Regards,

Praveen.

former_member237514
Participant
0 Kudos

Hi Praveen,

I am getting Quantity data from Mat_stock_Update as a string  but target maintain  as decimal type.i did one to one map then i test ted i am getting error like

"

String or binary data would be truncated.

The statement has been terminated."

former_member186851
Active Contributor
0 Kudos

Hell Kavitha,

Can you post an input example?

former_member182412
Active Contributor
0 Kudos

Hi Kavitha,

Use formatByNumber function.

Regards,

Praveen.

iaki_vila
Active Contributor
0 Kudos

Hi Kavi,

That exception sounds to me to SQL, are you working with jdbc adapter?

On the other hand you can find some links in java to do the conversion: Convert string to decimal number with 2 decimal places in Java - Stack Overflow

Also i recommend to use BigDecimal type that it is more accurate, like the 10 example of the Raghu's blog

Regards.

former_member237514
Participant
0 Kudos

still i am getting same error.

MP: exception caught with cause com.sap.engine.interfaces.messaging.api.exception.MessagingException: SOAP: Response message contains an errorXIAdapter/PARSING/ADAPTER.SOAP_EXCEPTION - soap fault: Server was unable to process request. ---> String or binary data would be truncated.

The statement has been terminated.

former_member237514
Participant
0 Kudos

Hi Inaki,

my scenario is IDOC - to -Soap

former_member182412
Active Contributor
0 Kudos

Hi Kavitha,

Check the length of the field in target side, i think you are sending more than what target is expecting it. And i just gave you the example to put 2 decimal places but you need to configure based target format.

Regards,

Praveen.

former_member237514
Participant
0 Kudos

Hi Parveen,

i am maintain same length  for that field .

former_member182412
Active Contributor
0 Kudos

Hi Kavitha,

What i am saying is if the target side the field maximum length is 10 characters but you are passing 12 characters at runtime then you will get above error.

Regards,

Praveen.

former_member186851
Active Contributor
0 Kudos

Kavitha if you can post the sample input and output values easily we shall help.,

GauravKant
Contributor
0 Kudos

Hi Kavitha,

Please use below UDF code for String to decimal converter.

Code:

public void calculate(String[] var1, ResultList result, Container container) throws StreamTransformationException

{

Double f=Double.parseDouble(var1[0]);

result.addValue(f);

}

Output:

Input is String and out is in double format.

Regards,

Gaurav

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Kavitha,

you can use either Double.parseDouble or Float.parseFloat in an UDF to solve it.

example:

double f= Double.parseDouble(var1[0]);

Float f=Float.parseFloat(var1[0]);

Thanks

Sagarika

former_member182412
Active Contributor
0 Kudos

Hi Sagarika/Inaki/Gourav,

formatNumber standard function internally uses BigDecimal only we no need to write any UDF for this requirement.


public String formatNumber(String _num, DecimalFormat _df, char _separator, Container _container)

  {

    DecimalFormatSymbols dfs = _df.getDecimalFormatSymbols();

    dfs.setDecimalSeparator(_separator);

    _df.setDecimalFormatSymbols(dfs);

    return _df.format(toBigDecimal(_num));

  }


public static BigDecimal toBigDecimal(String s)

  {

    s = s.trim();

    try

    {

      decimal = new BigDecimal(s);

    }

    catch (NumberFormatException e)

    {

      BigDecimal decimal;

      throw new FunctionException(RB_Functions.ARITHM_CANNOT_CAST(s), e);

    }

    BigDecimal decimal;

    return decimal;

  }

Regards,

Praveen.

GauravKant
Contributor
0 Kudos

Hi Praveen,

Thanks for the information!!

If i am not wrong, In that case we have to be sure about incoming fraction part length otherwise it will add or remove fraction part.

And If we are not sure about incoming fraction part length then  through UDF using Float or Double it will pass whole incoming data to target.

Regards,

Gaurav

former_member182412
Active Contributor
0 Kudos

Hi Gaurav,

Sender is IDOC so in ERP each IDoc field is assigned to domain, in the domain we define fixed decimal points.

Regards,

Praveen.

GauravKant
Contributor
0 Kudos

Hi Kavitha,

You can try using UDF.

Try,

Float f=Float.parseFloat(string);

for decimal value it will work perfectly.

Regards,

Gaurav

0 Kudos

Hi Kavitha,

Write small UDF with below code.

int no=Integer.parseInt(var1[0]);

result.addValue(no);

Regards,

Ajinkya

former_member182412
Active Contributor
0 Kudos

Hi Ajinkya,

She want it in decimal format but you are giving integer format solution.

Regards,

Praveen.

0 Kudos

Hi Kavitha,

Based on requirement you can type cast Integer/Float/Double.

I also imported java.lang in UDF

Regards,

Ajinkya