4

I am doing a little bit of mathematical calculations. Here is my code:

   decimal FirstYr = decimal.Round((first / second), 5);

If I am passing first = 20 and second = 34, I am getting the value of 0.58824 in FirstYr. Which is fine. Now I am doing this calculation here in my LINQ:

   ev.HiComm = (float)(FirstYr * 100);

HiComm is float in the DB and this above calculation is passing value: 58.824001312255859

Which I dont want it. I want in my ev.HiComm = 58.824

What I am doing wrong here?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
RG-3
  • 6,088
  • 19
  • 69
  • 125

2 Answers2

3

if you want exactly 3 places after the point, you'll have to change your sql field to be decimal(x,3) where x is 3 plus the maximum places you'll need before the decimal. float is incapable of storing the exact value you want.

lincolnk
  • 11,218
  • 4
  • 40
  • 61
  • process the value read from the db back into your desired format? if your value needs to be that precise, `float` is going to be insufficient. – lincolnk Apr 22 '11 at 16:58
  • @Mayank Raj: The only thing you can do is round the value back to 3 decimals whenever you fetch it from the DB. – Michael Borgwardt Apr 22 '11 at 16:59
  • 2
    @lincolnk: Lack of precision is not the problem. The problem is wanting to store decimal fractions in a binary format. – Michael Borgwardt Apr 22 '11 at 17:01
  • @Michael: It actually is for creating reports/jobs in MSSQL 2008. I cannot do that. The only option I have is to either change the datatype in DB or change something in my code behind... – RG-3 Apr 22 '11 at 17:07
  • @Mayank Raj: then change the DB datatype - that's the actually correct solution. – Michael Borgwardt Apr 22 '11 at 17:14
0

It worked like this in the code behind:

 decimal FirstYr = decimal.Round((first / second), 5)*100;
 ev.HiComm = (double)FirstYr ;

Thanks for all your suggestions!

RG-3
  • 6,088
  • 19
  • 69
  • 125
  • 1
    All you did was cast a decimal to a double which begs the question the reason you are using a float ( decmial ) value in the database to begin with. – Security Hound Apr 22 '11 at 17:51