473,385 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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)strStringVariable, but it does not compile. Help. Thanks.
Nov 16 '05 #1
3 15349
=?Utf-8?B?SGFycnkgS2Vjaw==?= 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)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/
Nov 16 '05 #2
string s = "333";

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

"Harry Keck" <an*******@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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)strStringVariable, but it does not compile. Help.
Thanks.
Nov 16 '05 #3
Harry Keck <an*******@discussions.microsoft.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)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.

--
Jon Skeet - <sk***@pobox.com>
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
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...
2
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
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...
3
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...
1
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...
3
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...
13
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...
2
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...
1
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: '...
3
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.