0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
John Ernest Guadalupe
  • 6,379
  • 11
  • 38
  • 71
  • Possible duplicate of [How to convert percentage string to double?](https://stackoverflow.com/questions/2171615/how-to-convert-percentage-string-to-double) – Andrey Belykh May 18 '18 at 16:53

4 Answers4

5

Maybe something like:

double value = double.Parse(p.TrimEnd(new[] {'%'}))/100;
Zaranitos
  • 101
  • 3
2

You can using split and cast it to double

double value = double.Parse(p.Split(new char[]{'%'})[0]) / 100;
Adil
  • 146,340
  • 25
  • 209
  • 204
1
   double value = double.Parse(p.Trim().Split('%')[0]) / 100;
H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    +1 Well, I would use Double.Parse(p.TrimRight(' ', '%')) to do _little bit_ more error checking (or "6%abc" may be considered valid) but it works! – Adriano Repetti Oct 18 '12 at 12:21
0
string p = "6%";
string p2 = p.Remove(p.Length - 1);
double value = Convert.ToDouble(p2) / 100;
Liam
  • 27,717
  • 28
  • 128
  • 190