De-currency format in C #

I have a string value coming from the label on the .aspx page as follows.

string text = Price.Text; // Price.Text = "$31.07" Single value = Convert.ToSingle(text); //throws FormatException 

I can replace the $ sign with empty text and then convert to single, but I was wondering if there is a better way to de-format text with the $$ sign to Single.

+4
source share
5 answers

Instead, you can do the following:

 string text = Price.Text; // Price.Text = "$31.07" Single value = Single.Parse(text, NumberStyles.Currency); 
+10
source

I would suggest that you remove the "$" as you suggest, and then use Single.Parse() to convert the string to a number.

0
source

Just do

 string text = Price.Text; text=text.Replace("$",""); Single value = Convert.ToSingle(text); 
0
source

If USD is your own currency of your computers, you can use:

 Single value = Single.Parse("$31.07", NumberStyles.Currency); 

If this is not so, I would go for sweeps.

0
source

With alternative crops:

 decimal.Parse("£12,345.67", NumberStyles.Any, new CultureInfo("en-GB")); decimal.Parse("€12.345,67", NumberStyles.Any, new CultureInfo("nl-NL")); 
0
source

Source: https://habr.com/ru/post/1303490/