473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How Do I cast a string to a Decimal?

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)strStr ingVariable, but it does not compile. Help. Thanks.
Nov 16 '05 #1
3 15354
=?Utf-8?B?SGFycnkgS2V jaw==?= wrote:
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)strStr ingVariable, 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(s trStringVariabl e);

--
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/
Nov 16 '05 #2
string s = "333";

decimal d = decimal.Parse( s );
--
Floyd Burger

"Harry Keck" <an*******@disc ussions.microso ft.com> wrote in message
news:A6******** *************** ***********@mic rosoft.com...
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)strStr ingVariable, but it does not compile. Help.
Thanks.
Nov 16 '05 #3
Harry Keck <an*******@disc ussions.microso ft.com> wrote:
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)strStr ingVariable, 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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14818
by: Mark | last post by:
I have a Decimal or a Double - your choice. I'd like to print the number as string with a specified number of decimal places. Let's say my number 110.5, and I'd like to capture it in a string with 3 decimal places: "110.500". How do you do this? My method below isn't working so hot. Decimal dcTotal = 110.5; string strMyString =...
2
23352
by: mp | last post by:
How do i cast a string from sql to a long? lDocumentVersion is long, oSqlDR.GetValue(0) returns a string. lDocumentVersion =oSqlDR.GetValue(0); Thanks
6
1670
by: Larry Lard | last post by:
I am trying to apply the following easy-to-state formatting rule to display a number: - If the number is positive or negative, display it in the 'default' way - If the number is zero, the output should be an empty string Now I know this is trivial to code, but the elegant solution would be passing a custom format string to ToString()....
3
6031
by: dbuchanan | last post by:
How do I explicitly cast a string to Byte? Here are my circumstances; The string is from a textbox and could be empty. The Byte is a tinyint in SQL Server 2000 I am using the AddRow method as shown below and get an InvalidCastException. "Cast from string "" to type integer is not valid" ...
1
9964
by: mdawoodk | last post by:
i am getting error "input string was not in correct format" when converting a string decimal into integer value. code is like this: string strVal = ""; int nVal = 0; strVal = "14.9"; nVal += Convert.toInt32(strVal);
3
5278
by: Dan Holmes | last post by:
i have a datareader that pulls a field of type numeric(18, 0). the debugger shows the field as {decimal}. I want to cast it to an int. This doesn't work (int)dr:. Is that because the definition of the column would overflow an int? So i would have to do something like int.Parse(dr.ToString()); dan
13
1787
by: archuleta37 | last post by:
I'm trying to cast a String from a web forms textbox into an Int16 (see code below), but it's not quite working. When I post the textbox with numbers (no alpha chars) I get the following error in my web page: Exception Details: System.FormatException: Input string was not in a correct format. String qty; foreach (GridViewRow row in...
2
13258
by: elena | last post by:
Hi, All Please, i need help with cast string to DateTime: DateTime dt = new DateTime(2006,09,17); it works i can update Field in Access DB Next field is Short Time data type, how i can cast string myTime = "13:30"; elena
1
2454
by: =?Utf-8?B?U2lzbmF6?= | last post by:
I'm having a very strange problem I can't seem to figure out and am hoping maybe somebody has seen it before. I get an exception "Cast string to date is invalid" with this chunk of code: ' class member Private m_invDate As Date ' this code is not exact from the program, but essentially does the same thing. This is were the exception...
3
3840
by: Mike Howarth | last post by:
Hi Seem to be having a bit of brainfreeze this evening. Basically I'm reducing an array of prices like so: This gives me a string of '86.00.00' which I am trying to use with decimal objects. Python 2.4 is not particularly happy with this. Additionally I can't simply convert the string to a decimal as it would be
0
7479
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.