John Ernest Guadalupe
John Ernest Guadalupe

Reputation: 6639

I want to turn a percentage value string to double?

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

Answers (4)

Adil
Adil

Reputation: 148178

You can using split and cast it to double

double value = double.Parse(p.Split(new char[]{'%'})[0]) / 100;

Upvotes: 2

Zaranitos
Zaranitos

Reputation: 101

Maybe something like:

double value = double.Parse(p.TrimEnd(new[] {'%'}))/100;

Upvotes: 5

Liam
Liam

Reputation: 29760

string p = "6%";
string p2 = p.Remove(p.Length - 1);
double value = Convert.ToDouble(p2) / 100;

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273864

   double value = double.Parse(p.Trim().Split('%')[0]) / 100;

Upvotes: 1

Related Questions