Forum guidelines
AuthorQuestion
C# Set precision
Posted on: 04 Nov 2011
I want to set the precision or make sure i am getting the precision by 2 decimal points, at the moment once I read in the value, its rounded off to the nearest dollar if you know
what i mean.

Can some assist me. I want to set the precision by two decimal places.

  if (reader["PRICE"] != DBNull.Value)
  {
  Row.PRICE= Convert.ToDecimal(reader["PRICE"]);
  }

AuthorReply
Re: C# Set precision
Posted on: 04 Nov 2011  
I am notn for sure if  the dataset is rounding off ronot, I just got stepping throught some of the vale, I am getting the decimal places, but when I look at it in the grid view the output is rounded to the nearest  dollar

Re: C# Set precision
Posted on: 06 Nov 2011  
Do the decimal conversion at sql statement level or you can use Math.Round or .ToString ("#.##")
Mark as Answer, on the posts reply's that helped you !!!.
Re: C# Set precision
Posted on: 06 Nov 2011  
In C#, the decimal type always has a precision of 28 or 29 digits.

How you display it is another matter.

As well as the methods which Suthish mentioned, you can also use the F2 format:

decimal d = 12.3456m;
MessageBox.Show(String.Format("{0:F2}", d)); // 12.35

Re: C# Set precision
Posted on: 07 Nov 2011  
So after stepping through the code. I am actually getting the decimal point. I wonder once I call the fill or add to the dataset, it rounds everything up. I am not for sure. I just know when i got to preview the data, everything is round to the nearest dollar, and there no decimal point

Re: C# Set precision
Posted on: 07 Nov 2011  
Try creating a strongly typed dataset and bind your records.
Mark as Answer, on the posts reply's that helped you !!!.
Re: C# Set precision
Posted on: 07 Nov 2011  
It sounds to me as if the problem lies with how you're formatting numeric strings in the GridView, rather than how you're retrieving them from the database.
Re: C# Set precision
Posted on: 07 Nov 2011  
Im not for sure if the dataset is defualted this way or not, but how would you fix it, I simply just added records. How to verify the precision to make sure it's behaving with decimal places.

Re: C# Set precision
Posted on: 07 Nov 2011  
If 'ds' is the DataSet and 'MyTable' is the name of the DataTable, then you can check whether the PRICE column is a decimal or not with this line:

bool b = ds.Tables["MyTable"].Columns["PRICE"].DataType == typeof(decimal);

Re: C# Set precision
Posted on: 07 Nov 2011  
I found the issue, in my table design,

my types were  decimal (18,0)  instead of decimal (18,2). I suck. I am so mad. I have beed tracing this for the last two days. Im glad i save this to the end. This would have killed me. lolololol

Re: C# Set precision
Posted on: 07 Nov 2011  
As I replied before, Do the decimal conversion at sql statement level .
Mark as Answer, on the posts reply's that helped you !!!.
Re: C# Set precision
Posted on: 07 Nov 2011  
Give me an example of what you are talking about. Even though I have fix the issue already setting the precision this way in the designer.

decimal (18,2)

Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.