on 09-27-2016 7:02 AM
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
Hi Kavitha,
Can you give us the example?
Regards,
Praveen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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
Hi Kavitha,
You can try using UDF.
Try,
Float f=Float.parseFloat(string);
for decimal value it will work perfectly.
Regards,
Gaurav
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.