-1

In my computer when I harcode a variable with 0.3(or maybe some other value) of value and I debug and check the variable's value , its value is 0.29999992 but in my friends computer, it stays in 0.3.

 //stores 0.29999992
double variable= 0.3;

is there a configuration problem or something related?

Thanks

Rubinoff
  • 25
  • 1
  • 6

2 Answers2

4

This is just an artifact of how binary floating-point works. There is no way of accurately representing 0.3 in a double (or a float for that matter). If you need that (e.g. for monetary applications), use decimal instead.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • But we have to use a lot of Math.Pow that is not possible with decimals only with doubble , what is your advice? – Rubinoff Feb 03 '16 at 12:20
  • Live with the fact that you don't have exact numbers. `0.3` in code, while not exactly `0.3` is still `0.3` if shown to 15 decimal places or so. You have plenty of precision for your calculation. Just be mindful of how much you show. – Joey Feb 03 '16 at 12:48
  • but why is 0.3 shown as 0.3 in my project and in my friends project as 0.22222229? is varies everytime? – Rubinoff Feb 03 '16 at 12:56
3

Welcome to the world of floating point numbers. Some, seemingly innocuous numbers cannot be represented exactly in floating point notation. A very close approximation is used instead.

Jens Meinecke
  • 2,904
  • 17
  • 20