Convert a string to double with negative sign

Hi, I am reading a value in SAP with the “get text” activity. Then I want to convert the variable string to double so that I can calculate with it. However, this does not work because there is sometimes a negative sign after the number.

image

how can I convert the value into a double?

Thank you in advance
Nina

1 Like

Hey @nina.wenner

You want to consider the negative sign or just to ignore ?

And I can see there is a comma as well… What to do on that…

Kindly confirm ?

Thanks
#nK

Hi

Mention like this in a assign activity

double_output = cdbl(str_var)*-1

Where double_output is a variable of type System.Double

This will have both minus and value of type double

Cheers @nina.wenner

Hi @nina.wenner

You can use this code to convert your value to double

Convert.ToDouble(YourString.replace(“-”,“”).trim)

YourString is string variable where you can store the output of get tex activity

Thank you

Hi!

Try this:

Convert.ToDouble(YourString.replace("-*","").trim)

Reference:
var1

Regards,
NaNi

grafik

 Double.Parse("1,0-", NumberStyles.Any, new CultureInfo("de-DE"))
 -1
 Double.Parse("-1,0", NumberStyles.Any, new CultureInfo("de-DE"))
 -1
 Double.Parse("123.45,34-", NumberStyles.Any, new CultureInfo("de-DE"))
 -12345.34

it is preferred to do such conversions with the API instead of with String Manipulations. Any failings/exception trackings allow to set up a more reliable implementation e.g. TryParse checks

1 Like

@ppr

image
i get an error message. cultureInfo is not defined.

Import system.globalization to the namespaces

Did this method work on your scenario

I got to see a similar scenario here which hot worked

Cheers @nina.wenner

I have used this variant → double_output = cdbl(str_var)*-1 - it works!!!

Thank you very much for your help.

Best regards
Nina

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.