How Do I cast a string to a Decimal?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would imagine that this is a pretty trivial operation, but I can not figure out how to cast a string variable to a Decimal. I tried Decimal dVariable = (Decimal)strStringVariable, but it does not compile. Help. Thanks.
 
=?Utf-8?B?SGFycnkgS2Vjaw==?= said:
I would imagine that this is a pretty trivial operation, but I can not
figure out how to cast a string variable to a Decimal. I tried
Decimal dVariable = (Decimal)strStringVariable, but it does not
compile. Help. Thanks.

You cannot CAST a string to a decimal... you only can CONVERT it...

Try:
decimal dVariable = decimal.Parse(strStringVariable);

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
Harry Keck said:
I would imagine that this is a pretty trivial operation, but I can
not figure out how to cast a string variable to a Decimal. I tried
Decimal dVariable = (Decimal)strStringVariable, but it does not
compile. Help. Thanks.

You can't cast it, because there's no implicit or explicit conversion
from String to Decimal, and Decimal isn't in String's type hierarchy.

Have a look at Decimal.Parse instead.
 
string s = "333";

decimal d = decimal.Parse( s );


--
Floyd Burger

Harry Keck said:
I would imagine that this is a pretty trivial operation, but I can not
figure out how to cast a string variable to a Decimal. I tried Decimal
dVariable = (Decimal)strStringVariable, but it does not compile. Help.
Thanks.
 
Back
Top