seleniumtester
seleniumtester

Reputation: 9

how to convert string ($23.25) to Decimal -$23.25 in selenium with C#

I am using xpath to get a string shown in a format like ($23.25) which should be converted to -$23.25. I have tried:

decimal.Parse(string, NumberStyles.AllowCurrencySymbol | NumberStyles.Number);

But this is not returning the negative value.

Upvotes: 1

Views: 274

Answers (1)

Xiaoy312
Xiaoy312

Reputation: 14477

Instead of NumberStyles.Number use NumberStyles.AllowParentheses.

var balance = decimal.Parse("($23.25)", NumberStyles.Currency | NumberStyles.AllowParentheses); // -23.25

If you would like to format it to "-$23.25", here is how :

balance.ToString("$0.00") // -$23.25

Upvotes: 4

Related Questions