0

I am new to c# and I am using windows forms. I want to subtract a value from label1.Text but I have problem with £ symbol

Let's say label1.Text has value with £ symbol like this: label1.Text = £10.5

Now I want to subtract a value from label1.Text but the issue that the £ is there. Let's say I want to subtract value of 5 from label1.Text as following:

double V = 5;
// now trim the £ sign from label1.Text
string s= Convert.ToString(label1.Text).Replace("£","");
// now s = 10.5 
double result = Convert.ToDouble(s); // it throws error here on this line
double Sum = result -v;

when I run the program it throws an error "Input string was not in correct format"

Anyone knows what I am doing wrong? Please help me how can I subtract a value from a string which contains £ symbol in correct way. Thank you

Kate
  • 935
  • 4
  • 14
  • 34
  • Which line throws the error? – ekad Mar 18 '16 at 12:22
  • I've marked it as duplicate. The other Q&A is about `decimal` type, but you should be able to get the same result with doubles... – Matías Fidemraizer Mar 18 '16 at 12:23
  • @ ekad. please see the update. tnx – Kate Mar 18 '16 at 12:25
  • @Kate...This works fine for me. Probably your `label1.Text` is empty or it has not the `£10.5` value! – Salah Akbari Mar 18 '16 at 12:27
  • @ Matías Fidemraizer . I do not understand why do not you allow other people to give more ideas/answers on here!!!! – Kate Mar 18 '16 at 12:28
  • Your code works fine as you can see [here](https://dotnetfiddle.net/NBlxe4), are you sure `label1.Text` actually has `£10.5` as its value? – ekad Mar 18 '16 at 12:28
  • @ S.Akbari . tnx for trying, no it is not empty I used messagebox.show() and it says 10.5 any idea? tnx – Kate Mar 18 '16 at 12:29
  • @Kate - click the link at the very top of your question. The answer at that link will fix this for you. (link is http://stackoverflow.com/questions/6946025/string-to-decimal-parsing-c-sharp) – Darren Wainwright Mar 18 '16 at 12:30

0 Answers0