Reputation: 6639
I have a percentage string in c# and want to return it into a double value.
for example . . .
string p = "6%";
Now I want to turn this string into
double value = 0.06;
How can I so that? I tried to use Math.Round() and put -2 in the digits to be rounded but it only allows numbers 0-15.
I am glad for any help you could offer.
Upvotes: 0
Views: 4671
Reputation: 148178
You can using split and cast it to double
double value = double.Parse(p.Split(new char[]{'%'})[0]) / 100;
Upvotes: 2
Reputation: 101
Maybe something like:
double value = double.Parse(p.TrimEnd(new[] {'%'}))/100;
Upvotes: 5
Reputation: 29760
string p = "6%";
string p2 = p.Remove(p.Length - 1);
double value = Convert.ToDouble(p2) / 100;
Upvotes: 0
Reputation: 273864
double value = double.Parse(p.Trim().Split('%')[0]) / 100;
Upvotes: 1