Decimal to floating casting

I do some math calculations. Here is my code:

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

If I pass first = 20 and second = 34 , I get the value 0.58824 in FirstYr . This is normal. Now I am doing this calculation here in LINQ :

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

HiComm is a float in DB , and this transfer value is calculated above: 58.824001312255859

Which I do not want this. I want to ev.HiComm = 58.824

What am I doing wrong here?

+4
source share
2 answers

It worked as follows:

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

Thanks for all your suggestions!

0
source

if you want exactly 3 places after the dot, you will need to change your sql field as decimal(x,3) , where x is 3 plus the maximum places you need before the decimal. float cannot store the exact value you want.

+3
source

All Articles