Need help with String to Double data type conversion

Hi,

I am getting following error when trying to convert to Double.

Can not assign ‘output_dt.AsEnumerable.Sum(Function(a) If(String.IsNullOrEmpty(a(“Opening Deposit”).ToString),0,CDbl(a(“Opening Deposit”))))’ to ‘OpeningDeposit’. —> System.InvalidCastException: Conversion from string “$-11,534.00” to type ‘Double’ is not valid

Hi @minesh.kumar ,

I think the issue is because of the $ and ,

Use an assign activity and use system.replace to remove the $ and , from the string.

Then try converting to double.

Hope it helps.

If this helps please mark as solution, if you need any further assistance also let me know.

Happy automating.!

Thanks,
Gautham.

Hi @Gautham_Pattabiraman - I am aggregating column values using LINQ as below. Where can I replace that?

output_dt.AsEnumerable.Sum(Function(a) If(String.IsNullOrEmpty(a(“Opening Deposit”).ToString),0,CDbl(a(“Opening Deposit”))))’

Hi @minesh.kumar

Try this:
openingDepositTotal = output_dt.AsEnumerable().Sum(Function(a) If(String.IsNullOrEmpty(a("Opening Deposit").ToString()), 0, CDbl(a("Opening Deposit").ToString().Replace("$", "").Replace(",", ""))))

openingDepositTotal is of DataType System.Double

Regards

Understanding the Issue:

  • The error arises because the string values in the “Opening Deposit” column contain a currency symbol (“$”) and commas, which aren’t directly parsable as doubles.
  • CDbl cannot handle these extra characters.

Solution:

  1. Remove Invalid Characters:

    • Use String.Replace to remove “$” and “,” before conversion:
    Dim cleanValue = a("Opening Deposit").ToString().Replace("$", "").Replace(",", "")
    
  2. Utilize Double.TryParse:

    • This method attempts conversion and handles potential errors:
    Dim openingDeposit = output_dt.AsEnumerable()
                                 .Sum(Function(a)
                                        If(String.IsNullOrEmpty(a("Opening Deposit")),
                                           0,
                                           Double.TryParse(cleanValue, Nothing) ? cleanValue : 0))
    

Explanation:

  • String.Replace removes “$” and “,”, creating a parsable string.
  • Double.TryParse checks if conversion is possible.
    • If successful, it assigns cleanValue to openingDeposit.
    • If not, it assigns 0 to avoid errors.

Additional Recommendations:

  • Data Cleaning: Ensure data consistency to minimize conversion errors.
  • Validation: Validate data types before operations.
  • Custom Cultures: Use CultureInfo.GetCultureInfo("en-US") for currency conversions.
  • Alternative Methods: Explore Decimal.TryParse or regular expressions for complex scenarios.

Best Practices:

  • Handle potential errors gracefully.
  • Consider different parsing techniques based on data formatting.
  • Thoroughly test your code to prevent unexpected issues.

By following these guidelines, you can ensure accurate and robust data type conversions in your UiPath C# projects.

1 Like

Thanks @vrdabberu . It worked…!!!

1 Like

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