Question

Example of my string value

string amtStr = "{100,000,000.00}";

Expected result/output

double amt = -100,000,000.00

What I've tried but it gives me error "Cannot convert from 'string' to 'System.IFormatProvider'"

Convert.ToDouble(txtAmount.Text.ToString("#,0.00;-#,0.00"))
Was it helpful?

Solution

Try this code

        string amtStr = "{100,000,000.00}";
        amtStr = amtStr.Replace("{", "-").Replace("}", "").Replace("(", "-").Replace(")", "").Trim() ;
        double amt = Convert.ToDouble(amtStr);

OTHER TIPS

Without replacing that braces {} this by default cannot be converted. If it was a proper accounting number format then you can use NumberStyles like this

string amtStr = "(100,000,000.00)";
NumberStyles styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign | NumberStyles.Float | NumberStyles.AllowThousands;
string outputStr = double.Parse(amtStr, styles).ToString("#,0.00;-#,0.00");
double outputDbl = double.Parse(outputStr);

Based on the title of the question, maybe what you need is:

amt = Double.Parse(amtStr.Trim("{}".ToCharArray()))

Here, we remove leading and trailing { and }s and then parse the resulting string to a double using Double.Parse. ToCharArray is included so we can specify chars to be removed as one string rather than a char array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top