473,394 Members | 1,742 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,394 software developers and data experts.

cast from decimal to int

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["field1"]:. Is that because the definition of
the column would overflow an int? So i would have to do something like
int.Parse(dr["field1"].ToString());

dan
Jul 7 '06 #1
3 5271
Dan Holmes wrote:
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["field1"]:. Is that because the definition of
the column would overflow an int? So i would have to do something like
int.Parse(dr["field1"].ToString());
I believe that you have to cast in two steps... that you can't unbox
the value and cast it all in one go. If you say

decimal f1 = (decimal)dr["field1"];

then this will unbox the value, then you can say:

int i1 = (int)f1;

to do the cast. You might even be able to do this:

int i1 = (int)((decimal)dr["field1"]);

but I'm not 100% sure without trying it myself.

Jul 7 '06 #2
Dan Holmes <da*******@bigfoot.comwrote:
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["field1"]:. Is that because the definition of
the column would overflow an int? So i would have to do something like
int.Parse(dr["field1"].ToString());
It's because the DataRow indexer is declared to return Object. The cast
is therefore treated as an unboxing conversion, which has to be of the
exactly correct type. What you want is to unbox to decimal and then
cast to int:

(int)(decimal)dr["field1"]

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 7 '06 #3
You could also try Convert.ToInt32(dr["FieldName"])
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Dan Holmes" wrote:
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["field1"]:. Is that because the definition of
the column would overflow an int? So i would have to do something like
int.Parse(dr["field1"].ToString());

dan
Jul 8 '06 #4

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

Similar topics

0
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
3
by: Mike | last post by:
I am using MS-Access as a front end for my MS-SQL DB. I have a sql view that uses the following: SELECT TOP 100 PERCENT RECID, PATNUMBER AS , SVCCODE AS , QTY, PROF_CHRGS AS , AMOUNT,...
3
by: Harry Keck | last post by:
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...
6
by: Nick Weekes | last post by:
Hi all, I am trying to cast a Double (its actually the NextDouble method of the Random class, but could be any type of number) to a Type via its name. Im trying the following syntax: ...
3
by: .Net Sports | last post by:
I have a variable that is part of my dataset (a double int datatype), and I need to use this variable in an ItemDataBound for a datagrid. When debugging and looking at the StackTrace, the Specified...
1
by: .Net Sports | last post by:
IN a datagrid code behind, I'm getting a "Specified cast is not valid" error price = (Decimal)(rowData); which was initialized as: Decimal price;
4
by: TheSteph | last post by:
Why do I get an exception "The specified cast is not valid" ??? Is that a bug or do I miss something ? object DecimalObject = 1; decimal TmpDecimal; TmpDecimal = (decimal)DecimalObject;
1
by: skhuon | last post by:
Hi, when I divide Sum (AMT_NET_GAAP_GNLS)/Sum (AMT_UPB_FM) and these two fields are decimal fields, the results is 0 I know there is a way to cast the decimal point but i keep on getting errors,...
2
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all how can I write a method that returns me true if is possible to cast an object to decimal nullable and false if not? Like public bool CanConvert(object obj) { return true if obj is...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.