Parsing %-values

  • Thread starter Thread starter Christian Nein
  • Start date Start date
C

Christian Nein

Hi,
I am trying to parse percent values (eg. 12.9%) with the double.parse-function.
double d = double.Parse("12.9%");
Unfortunately, this always throws an exception.

What is the problem?

Regards,
Christian
 
what exception?

--


-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
Hi,
I am trying to parse percent values (eg. 12.9%) with the double.parse-function.
double d = double.Parse("12.9%");
Unfortunately, this always throws an exception.

What is the problem?

Regards,
Christian
 
It says that the string is not in a correct format. I guess this is a culture problem as I have a German OS, but the thread in which I try to parse is English...

Christian
"Alvin Bruney":
what exception?

"Christian Nein":
Hi,
I am trying to parse percent values (eg. 12.9%) with the double.parse-function.
double d = double.Parse("12.9%");
Unfortunately, this always throws an exception.

What is the problem?

Regards,
Christian
 
here is the man of the function public static double Parse(string s);

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcoricsharptutorials.asp

nothing says that the char '%' is authorised in the string s.
the error you have is that you try to parse the char '%'

insteed of this :
double a = Double.Parse("1.25%");
simply use this :
double a = Double.Parse("1.25") / 100;

ROM

"Christian Nein" <[email protected]> a écrit dans le message de news: (e-mail address removed)...
Hi,
I am trying to parse percent values (eg. 12.9%) with the double.parse-function.
double d = double.Parse("12.9%");
Unfortunately, this always throws an exception.

What is the problem?

Regards,
Christian
 
I think the problem is he has a string that contains a % sign so he does not
have the advantage of performing that type of parse.

You may have to look to see if the string contains a % and if it does, strip
it off, parse the value and then devide by 100.

Joe Feser

here is the man of the function public static double Parse(string s);

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcoricsharptutorials.asp

nothing says that the char '%' is authorised in the string s.
the error you have is that you try to parse the char '%'

insteed of this :
double a = Double.Parse("1.25%");
simply use this :
double a = Double.Parse("1.25") / 100;

ROM

"Christian Nein" <[email protected]> a écrit dans le message de (e-mail address removed)...
Hi,
I am trying to parse percent values (eg. 12.9%) with the
double.parse-function.
double d = double.Parse("12.9%");
Unfortunately, this always throws an exception.

What is the problem?

Regards,
Christian
 
Back
Top