Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

math.round not working with doubles

Tags:

c#

my doubles get not rounded as expected. Simple example:

 int b = 23;
 double DurchflussAktBit = 99.5;
 double bDurchfluss = 0;

 bDurchfluss = DurchflussAktBit * Convert.ToDouble(b) / (double)60;
 Math.Round(bDurchfluss, 2);

I get the value 38.141666666666666 for bDurchfluss even after the rounding, I expect the value 38.14. Also tried Math.Round((decimal)bDurchfluss, 2); but gives me the same value.

Where is the error in my code?

like image 685
Kᴀτᴢ Avatar asked Dec 09 '25 18:12

Kᴀτᴢ


1 Answers

Math.Round returns the rounded number - it does not update the number you passed it.

You need to take the return value and assign it to your variable:

 bDurchfluss = DurchflussAktBit * Convert.ToDouble(b) / (double)60;
 bDurchfluss = Math.Round(bDurchfluss, 2);
like image 157
Alex Booker Avatar answered Dec 11 '25 07:12

Alex Booker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!