2

I have this code in C#

double result = 480 - 460.8;

Why result is 19.199999999999989 instead of 19.2?

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Elham Azadfar
  • 709
  • 2
  • 17
  • 34

1 Answers1

9

you should format your double precision of the result output:

double result = 480 - 460.8; 
String.Format("{0:0.##}", result);

test example:

https://ideone.com/27OfP4

update:

there is another way without string formatting you can use method Math.Round with two digits after decimal place :

Math.Round(result,2);

example:

https://ideone.com/2Q6RPD

Oghli
  • 2,200
  • 1
  • 15
  • 37
  • @Elham Azadfar read this tutorial about round method for double numbers : https://www.dotnetperls.com/math-round – Oghli Mar 05 '17 at 08:54