TestProject Forum

How can I convert a string to number in C#?

For example:

  • Get text: -$1,170.00 (USD)
  • Trim(’ ', ‘$’, ‘(’, ‘U’, ‘S’, ‘D’, ‘)’)
  • Output: 1,170.00
    Here is the source code i wrote but it does not work. Does anyone can help me?:
    //lblCurrentBalance is an ID element
    string getCurrentBalance = driver.TestProject().GetText(lblCurrentBalance).Trim(’ ‘, ‘$’, ‘(’, ‘U’,‘S’,‘D’,’)’);
    float currentBalance = float.Parse(getCurrentBalance , NumberStyles.AllowCurrencySymbol);

Hello @tranngocha70,

Use NumberStyles.Currency instead of NumberStyles.AllowCurrencySymbol

float currentBalance = float.Parse(getCurrentBalance , NumberStyles.Currency);

Let me know if that works