-1

C# adds a decimal at the end in the result, my code:

public static double CalcCompoundedInterest()
{
    return (1.1 * 1.1);
}

Result: 1.2100000000000002

Does someone have a clue why this happens?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ceesz
  • 86
  • 1
  • 1
  • 10
  • 1
    This issue is called floating point error and exists in all float point arithmetic. To avoid this issue (especially when dealing with variables reflecting money) languages like C# provide a fixed point type like `decimal` – Vikhram Jan 24 '19 at 18:45

1 Answers1

5

This is not a C# problem, this is the way computers work when they handle decimal values.

You see, 1.1 is stored as a float, which is encoded in binary using the IEEE 754 standard. Most decimals numbers are not possible to store without adding a very small error to them.