Why does my double round my variables sometimes? [duplicate]

I really dont unterstand why, but my double array just sometimes round my variables, even though it shouldn't. The weird thing is, that it only does this sometimes, as you see in the picture. All of the array elements should have this long precision. The Variable "WertunterschiedPerSec" is also at this precision every time, but still, if i add it to Zwischenwerte[i], then it sometimes just get less precisie, even though i dont do anything anywhere. Does anybody know why?

the weird "bug"

3

2 Answers

I would suggest using a decimal, but let's get into the exact details:

double, float and decimal are all floating point.

The difference is double and float are base 2 and decimal is base 10.

Base 2 numbers cannot accurately represent all base 10 numbers.

This is why you're seeing what appears to be "rounding".

I would use the decimal variable instead of double because you can basically do the same functions as a double except for using the Math function. Try to use Decimals and if you need to convert than use:

Double variable = Convert.ToDouble(decimal var); 

Decimals are meant for decimals so they will hold more information than a float or decimal

You Might Also Like