Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot return full decimal from sql parameters

Status
Not open for further replies.

MJV57

Programmer
Apr 18, 2009
87
CA
I have a line of code as follows:
totalhoursTextBox.Text = (Convert.ToString(cmd.Parameters["@totalhrs"].Value.ToString()));

This code always truncates the digits after the decimal. Can anyone help with what is required to change to get the digits after the decimal. Eg. Now I get 41 when I should get 41.3.
 
Code:
Convert.ToString(cmd.Parameters["@totalhrs"].Value.ToString())
is redundant.
Code:
cmd.Parameters["@totalhrs"].Value.ToString();
will get the string value for you (assuming it's never null);

as for the conversion problem. this code isn't causing the rounding issue. it's most likely the sql statement that's causing the rounding. to confirm this
Code:
var value = cmd.Parameters["@totalhrs"].Value
then use the debugger to view the value assigned to [tt]value[/tt]. it will be 41.

if you are performing mathematical operations between different data types (for example an integer and a decimal) the data type with less precision will be used for the result. integer has less precision than decimal. to compensate for this convert the integer to a decimal and then preform the calculation.
Code:
select @totalhrs = convert(decimal(5,2), integer_field) / decimal_field

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I have checked the sql out put and it does give out numbers after the decimal.
 
Not wanting to seem impertinent, but what is the data type of the parameter?
 
Using this code, what does the MessageBox show?

Code:
if(cmd.Parameters["@totalhrs"].Value != DBNULL.Value)
{
   object oDec = cmd.Parameters["@totalhrs"].Value;
   decimal dec = decimal.Parse(oDec.ToString());
   MessageBox.Show(dec.ToString("G"));
}
else 
{
   MessageBox.Show("It was NULL!!"));
}

Age is a consequence of experience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top