Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double type precision in C# [duplicate]

Tags:

c#

types

double

I have this code in C#

double result = 480 - 460.8;

Why result is 19.199999999999989 instead of 19.2?

like image 873
Elham Azadfar Avatar asked May 07 '26 00:05

Elham Azadfar


1 Answers

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

like image 91
Oghli Avatar answered May 09 '26 13:05

Oghli



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!